Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=61003 Ensure the flags for reading/writing are correctly reset even if some exceptions occurred during processing.