patch from Stephen Fry in case of errors, input stream is closed. PR: Obtained from: Submitted by: Reviewed by: