Fix resource cleanup in pipe