ending the output properly when the result stream ends