Skip to content

Recover early responses after request-write connection resets - #365

Open
blaknite wants to merge 1 commit into
ruby:masterfrom
blaknite:recover-early-response
Open

blaknite wants to merge 1 commit into
ruby:masterfrom
blaknite:recover-early-response

Conversation

@blaknite

Copy link
Copy Markdown

Description

A server can send a final response, such as 413 Content Too Large, while Net::HTTP is still uploading the request body. If the connection closes during the upload, the write can raise ECONNRESET before Net::HTTP reads that response.

Net::HTTP already attempts to read the response after a write-side EPIPE, but does not do the same for ECONNRESET. For POST, the caller receives the reset even when the server’s response remains readable.

This change attempts to recover that response without resending the request.

Reproduction

  1. Start a local HTTPS server that records request counts and reads only the request headers. Have it send a complete 413 response, then close the underlying TCP socket without consuming the body or sending TLS close_notify.

  2. Use Net::HTTP to POST a large synthetic JSON string to that server. Keep certificate verification enabled and leave Expect: 100-continue disabled.

  3. Capture raised exceptions, for example with Ruby’s TracePoint, and repeat until the upload encounters a write-side ECONNRESET. Runs that only encounter EPIPE do not exercise this gap because Net::HTTP already handles that case.

  4. Compare the parent revision with this branch:

    • On the parent revision, the reset propagates without exposing the available 413.
    • With this change, the caller receives the 413 response and body when they remain readable.
    • The server observes only one POST.

Changes

A private IO wrapper records errors from destination writes, distinguishing them from errors reading streamed or multipart upload sources. Upload-source resets continue to propagate through the existing failure path.

Response recovery requires a final status and complete headers. If those cannot be recovered, the original reset is re-raised. Normal response-body errors, callback errors, and existing retry rules remain unchanged.

After a destination-write reset, the affected connection is closed in ensure, including when a callback exits nonlocally or a deadline interrupts recovery. Normal reads and the existing EPIPE path retain their EOF tolerance.

The regression tests inject failures deterministically rather than depending on the timing-sensitive network reproduction.

The network reproduction was performed on macOS. Ruby 2.7, Linux, and Windows runtime coverage remains outstanding.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant