Correctly close pipe