fix #998 to close the stream when deadlock