fix close stream logic