Repository navigation
Fix HTTP1Connection reuse crash after request outlives response - #929
Merged
Merged
Conversation
### Motivation
If the response finishes before the request body stream, we use the write promise of the request's `.end` to tell the connection pool that the connection can be reused. This write promise can be completed synchronously inside `writeAndFlush`. In that case the pool may schedule the next request onto the connection before `writeAndFlush` returns. Afterwards we transitioned the idle read/write timeout state machines, which by then belonged to the *new* request. With an idle read timeout configured, this crashes with:
HTTP1ClientChannelHandler.swift: Fatal error: Invalid state. Waiting for response data must start after request head was sent
This is a regression introduced in 1.31.0 (swift-server#879).
### Changes
- Update the idle read and write timeout state machines *before* writing and flushing the request's `.end`, so they are always applied to the request that is actually ending.
- Add a Swift Testing reproducer using `EmbeddedChannel` that schedules a new request from `onConnectionIdle` while the `.end` write completes.
- Allow `MockHTTPExecutableRequest` to be created with custom `RequestOptions`.
### Result
A request whose response finishes before its body stream no longer crashes the connection when the pool immediately reuses that connection.
weissi
approved these changes
Sep 25, 2026
weissi
left a comment
Contributor
There was a problem hiding this comment.
Thanks, this looks good.
State reconciliation before callouts to arbitrary code is definitely the correct choice here.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
If the response finishes before the request body stream, we use the write promise of the request's
.endto tell the connection pool that the connection can be reused. This write promise can be completed synchronously insidewriteAndFlush. In that case the pool may schedule the next request onto the connection beforewriteAndFlushreturns. Afterwards we transitioned the idle read/write timeout state machines, which by then belonged to the new request. With an idle read timeout configured, this crashes with:This is a regression introduced in 1.31.0 (#879).
Changes
.end, so they are always applied to the request that is actually ending.EmbeddedChannelthat schedules a new request fromonConnectionIdlewhile the.endwrite completes.MockHTTPExecutableRequestto be created with customRequestOptions.Result
A request whose response finishes before its body stream no longer crashes the connection when the pool immediately reuses that connection.