Handling stream closure before end of iteration