Skip to content

HttpRequester response entity writeTo returns normally after an I/O failure #1549

Done

Description

@AlwaysPS

In Apache HttpCore 5.5, a caller copying a response body with response.getEntity().writeTo(output) can receive a normal return even when the copy failed. If some bytes have already been written, the caller has no indication from writeTo that the output is incomplete.

This report is based on the 5.5 source. HttpRequester.execute wraps the response entity and delegates the copy to the wrapped entity. If the copy throws IOException, the wrapper catches it, calls abortConnection() to discard the connection, and reaches the end of writeTo without propagating the exception.

HttpEntity.writeTo documents IOException for an I/O error. The wrapper should discard the failed connection and propagate the original exception to the caller. A regression test could use an output stream that throws a known IOException and check that the caller receives that exception and that the connection is discarded.

Response wrapper in HttpCore 5.5

@Override
public void writeTo(final OutputStream outStream) throws IOException {
try {
if (outStream != null) {
super.writeTo(outStream);
}
close();
} catch (final IOException | RuntimeException ex) {
abortConnection();
}
}

HttpEntity.writeTo contract in the same release

* all allocated resources are properly deallocated when this method
* returns.
* </p>
*
* @param outStream the output stream to write entity content to
*
* @throws IOException if an I/O error occurs
*/
void writeTo(OutputStream outStream) throws IOException;

This report was drafted with AI assistance.

Activity

  1. self-assigned this
    on Dec 2, 2026
  2. ok2c commented on Dec 2, 2026

    @ok2c
    Member

    @AlwaysPS Please see the proposed fix #1551

  3. AlwaysPS commented on Dec 3, 2026

    @AlwaysPS
    Author

    Thanks, @ok2c. I verified #1551 at f512349 with a local HTTP server and an output stream that fails after writing three bytes. On 5.5 and on the parent commit, the response entity’s writeTo returned normally with only those three bytes copied. With the patch, the caller received the original IOException. I also checked the runtime exception case and got the original exception there as well.

    In both failure cases the connection was discarded, and the next request succeeded. Normal writes still copied the full response body. This fixes the behavior I reported in #1549.

  4. added a commit that references this issue on Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions