Skip to content

Stop polling response bodies after client disconnect - #3023

Closed
behnam-oneschema wants to merge 1 commit into
rwf2:v0.5from
oneschema:devin/upstream-response-cancel-1795027731
Closed

behnam-oneschema wants to merge 1 commit into
rwf2:v0.5from
oneschema:devin/upstream-response-cancel-1795027731

Conversation

@behnam-oneschema

Copy link
Copy Markdown

Fixes #3022.

While _send_response waits for the next item from the response body stream, it now also waits for Hyper to drop the body's receiver:

while let Some(next) = tokio::select! {
    biased;
    next = stream.next() => next,
    _ = sender.closed() => return Err(io::Error::new(
        io::ErrorKind::BrokenPipe, "client disconnect during response body"
    )),
} { /* unchanged: forward data, or send the reader error to Hyper */ }

Previously, a body reader that stalled was kept and polled after the client was gone. Rocket would only notice when the reader produced another item and the send failed, and a reader that never produced one stayed alive until shutdown.

  • The biased order checks the reader first. Data, EOF, and reader errors that are ready are handled exactly as before, so the error propagation from 1269fc3 is unchanged. Only a reader that is pending when the receiver closes is cut short.
  • When the receiver closes first, send_response returns BrokenPipe, which it already logs as "Remote left: …". It doesn't log "Response succeeded."

Tests

The new core/lib/tests/response-cancellation.rs launches a real server on 127.0.0.1. Its route streams hello and then stalls, and the test checks that the reader is dropped within 2 seconds:

  • http1_disconnect_drops_stalled_reader: a raw TCP client reads the status line and hello, then closes the socket.
  • http2_reset_drops_stalled_reader (#[cfg(feature = "http2")]): a Hyper prior-knowledge HTTP/2 client receives hello and drops the response while keeping the connection open. A second request on the same connection then succeeds.

Both tests time out without the server.rs change and pass with it. These also pass: --features http2, --no-default-features (HTTP/1.1 test only), --test response-framing, and ./scripts/test.sh --core (all 8 feature configurations).

Previously, while waiting for the next response body item, Rocket did
not observe that Hyper had dropped the body's receiver. A body reader
that stalled was thus retained and polled after its client was gone:
an HTTP/1.1 connection that closed or an HTTP/2 stream whose response
was dropped left the reader alive until the server shut down.

This commit also waits on the body channel's closure, preferring ready
body items so that EOF and reader error handling are unchanged. When
the receiver closes first, the response ends with a 'BrokenPipe' error
and the body, including its reader, is dropped.

Fixes rwf2#3022.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@SergioBenitez

Copy link
Copy Markdown
Member

Merged in 10fe7ef. Thank you!

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.

2 participants