correctly handle stream end event