Skip to content

Close connections still open after the shutdown cut-off - #251

Merged
saworbit merged 1 commit into
mainfrom
fix/s3-shutdown-cuts-off-streams
Nov 15, 2026
Merged

saworbit merged 1 commit into
mainfrom
fix/s3-shutdown-cuts-off-streams

Conversation

@saworbit

Copy link
Copy Markdown
Owner

After the grace period, S3Server::shutdown_on cuts off running handlers with a 503. A response that was already returned has no handler left to cancel. A GET whose body was still streaming kept its connection task, and the Arc<S3View> and sled lock it holds, alive after run() returned. That matters for code that embeds S3Server and keeps its runtime. spacectl serve-s3 exits right after run(), so it wasn't affected.

Why not wrap the response body, as the issue proposed. When a client stops reading, hyper is pending in poll_flush on the socket and doesn't poll the body, so a body that ends at the cut-off never gets the chance. The connection's IO is where the wait happens, so that's where the cut-off goes.

What changed. In the shutdown path each listener is a ClosingListener, whose connections are ClosingIo: a TcpStream that fails every read and write with ConnectionAborted once a close token is cancelled. Each poll first polls the token's owned future, so a write waiting on the client is woken when the token fires. run() now:

  1. cuts off running handlers with a 503, as before;
  2. waits up to 1 s for connections to close, as before;
  3. cancels the close token for any still open, and waits up to 1 s more.

Response bodies aren't touched, so they keep their size hint and Content-Length. The 503s go out before any connection is closed. Worst case, a stop takes 1 s longer than before.

Test. a_response_still_streaming_after_the_grace_period_is_closed PUTs 24 MiB, starts a GET from a client with a 4 KiB receive buffer, checks the head has content-length and no transfer-encoding, reads 64 KiB, then stops reading and signals. The store opens again once run() returns, while the client still holds its end. With close.cancel() removed, the reopen fails on the sled lock. Ran the shutdown suite three times.

CHANGELOG updated.

Fixes #239

After the grace period, S3Server cuts off running handlers with a 503.
A response already returned has no handler to cancel. A GET body still
streaming, or one the client stopped reading, kept its connection task,
and the S3View and store it holds, alive after run() returned.

The listener now wraps each connection in an IO type that fails every
read and write once a close token is cancelled, and wakes a write
waiting on the client. run() cancels it one second after the cut-off,
then waits up to another second for the connections to go.

Wrapping the response body instead would not work for a client that
stopped reading: hyper waits on the socket and never polls the body.

Fixes #239
@saworbit
saworbit enabled auto-merge (squash) November 15, 2026 08:28
@saworbit
saworbit merged commit de875a5 into main Nov 15, 2026
16 checks passed
@saworbit
saworbit deleted the fix/s3-shutdown-cuts-off-streams branch November 15, 2026 08:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[S3] Cut off response bodies still streaming at the shutdown deadline

1 participant