use end-of-stream to handle errors and all end events correctly