Repository navigation
Fix HTTP/2 hang on reused streams when a window update races the final write - #69690
Conversation
Http2FrameWriter skipped producers whose response had already completed without clearing Http2OutputProducer._isScheduled. A stream WINDOW_UPDATE racing a response's final write can schedule the producer again after ObserveDataAndState cleared the flag, so that queue entry was dropped with the flag still set. StreamReset doesn't reset the flag, so the next request that reused the pooled stream could never schedule its HEADERS, DATA or trailers, and its aborts were never processed. Unschedule a completed producer under its lock when the frame writer skips it, and continue with the next producer. Co-authored-by: Copilot App <[email protected]>
|
Thanks for your PR, @danield137. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
|
@dotnet-policy-service agree company="Microsoft" |
There was a problem hiding this comment.
Copilot review overview
🔵 Needs a closer look
The concurrency-sensitive stream-reuse change needs final human review because independent verification was unavailable.
Review effort: Balanced
Findings: None
What changed in this PR
Addresses #53736 by preventing stale scheduling state from blocking responses on reused Kestrel HTTP/2 streams.
Changes:
- Clears completed producers’ scheduling state under the existing lock and continues draining the writer queue.
- Adds regression tests for scheduling during and after response completion, including small and large responses.
| File | Description |
|---|---|
| src/Servers/Kestrel/test/InMemory.FunctionalTests/Http2/Http2ConnectionTests.cs | Tests pooled-stream reuse after late scheduling. |
| src/Servers/Kestrel/Core/src/Internal/Http2/Http2OutputProducer.cs | Adds atomic unscheduling for completed responses. |
| src/Servers/Kestrel/Core/src/Internal/Http2/Http2FrameWriter.cs | Unschedules completed producers before skipping them. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
|
We built Kestrel.Core from the .NET 10.0.12 sources with and without this patch and tested both using isolated runtime copies on Windows x64 (Release). The HTTP/2 echo test used 64 KiB payloads and one reused h2c connection, without the 16 MiB window workaround.
All completed responses were verified byte-for-byte. No timeouts or payload mismatches occurred with the patch. Our relay test suite also passed: 131/131 tests. We confirm that this fix resolves the hang in our reproduction. Thanks for the fix! |
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
|
PR is waiting for review. |
|
We are experiencing the same issue on our side. Looking forward to a fix. Thanks for looking into it! 🚀 |
|
Commenter does not have sufficient privileges for PR 69690 in repo dotnet/aspnetcore |
halter73
left a comment
There was a problem hiding this comment.
Thanks for the thorough investigation and well thought-out fix!
|
/backport to release/11.0 |
|
Started backporting to |
|
/backport to release/10.0 |
|
Started backporting to |
|
✅ No documentation PR was created. Confidence that documentation is needed: 5% No docs needed: internal Kestrel HTTP/2 bug fix restoring expected behavior. Documentation surfaces considered
|
|
@halter73 thank you for looking at this PR. |
|
/backport to release/8.0 |
|
Started backporting to |
… a reused stream (dotnet/aspnetcore#69690, not yet in 10.0): the stream's stale schedule flag is cleared when a request starts, the workaround turns itself off once Kestrel has the fix, and a test makes the race happen every time
…9690) (#70099) Http2FrameWriter skipped producers whose response had already completed without clearing Http2OutputProducer._isScheduled. A stream WINDOW_UPDATE racing a response's final write can schedule the producer again after ObserveDataAndState cleared the flag, so that queue entry was dropped with the flag still set. StreamReset doesn't reset the flag, so the next request that reused the pooled stream could never schedule its HEADERS, DATA or trailers, and its aborts were never processed. Unschedule a completed producer under its lock when the frame writer skips it, and continue with the next producer. Co-authored-by: Daniel Dror <[email protected]> Co-authored-by: Copilot App <[email protected]> Co-authored-by: Stephen Halter <[email protected]>
…9690) (#70100) Http2FrameWriter skipped producers whose response had already completed without clearing Http2OutputProducer._isScheduled. A stream WINDOW_UPDATE racing a response's final write can schedule the producer again after ObserveDataAndState cleared the flag, so that queue entry was dropped with the flag still set. StreamReset doesn't reset the flag, so the next request that reused the pooled stream could never schedule its HEADERS, DATA or trailers, and its aborts were never processed. Unschedule a completed producer under its lock when the frame writer skips it, and continue with the next producer. Co-authored-by: Daniel Dror <[email protected]> Co-authored-by: Copilot App <[email protected]> Co-authored-by: Stephen Halter <[email protected]>
Fix HTTP/2 hang on reused streams when a window update races the final write
Unschedule completed HTTP/2 producers so pooled streams can be reused
Description
Kestrel can reuse a pooled
Http2StreamwhoseHttp2OutputProducerstill has_isScheduledset but has no entry inHttp2FrameWriter's queue. EverySchedule()for the new request then returns early, so its HEADERS, DATA, and trailers are never written and its aborts are never processed. The client waits until its own timeout, nothing is logged, and other streams on the connection keep working. If the handler writes more than the stream's 4 KB output buffer, its write never completes. On connection close,Http2Connectionthen waits for that stream indefinitely.How the flag gets stuck
TryUpdateStreamWindowdecides to resume while holding_dataWriterLock, then callsScheduleResumeFromWindowUpdate()->Schedule()after releasing it.ObserveDataAndState()clears_isScheduled.Schedule()sets_isScheduledagain and queues the producer a second time. The_completedResponsecheck inScheduleResumeFromWindowUpdateisn't taken under the lock.while (TryRead(out producer) && !producer.CompletedResponse)drops it without clearing_isScheduled.StreamReset()doesn't reset_isScheduled, so the request that reuses the stream can never be scheduled.Fix
Http2FrameWriter.WriteToOutputPipenow calls the newHttp2OutputProducer.TryUnscheduleCompletedResponse()for each dequeued producer. If the response has already completed, that method clears_isScheduledand the writer skips the producer and continues with the next one. The check and the clear happen under_dataWriterLock, the same lockSchedule()uses, so a producer can't end up both unqueued and marked scheduled. The cost is unchanged, because the old loop condition took the same lock through theCompletedResponsegetter.Alternatives considered (evaluated against the new tests):
Schedule()return early once_completedResponseis set: this only covers a schedule that lands after completion. The "during final write" ordering still hangs._isScheduledinStreamReset(): this hides the symptom at reuse, but it leaves the skip path inconsistent. If the extra entry is still queued when the stream is reset, it also allows a second queue entry for the same producer.Tests
New tests in
Http2ConnectionTests(InMemory.FunctionalTests):StreamPool_ScheduledAfterResponseCompleted_PooledStreamReusedAndResponseSent(largeResponse: false|true): the producer is scheduled after its response completed, then the stream is pooled and reused for a response with headers, data, and trailers. The small response is buffered so the app completes without waiting on output. The large one (32 KB) awaits its writes.StreamPool_ScheduledDuringFinalWrite_PooledStreamReusedAndResponseSent: the first response's final write is held at its flush, soScheduleResumeFromWindowUpdate()lands afterObserveDataAndState()and beforeCompleteResponseAsync(). The test then lets the writer complete the response and dequeue the extra entry, and reuses the stream.Without the product change, all three cases time out waiting for the reused stream's HEADERS (5 of 5 runs). With it, they pass (20 of 20 runs).
Other validation on Windows x64 against main (16e71d2):
Microsoft.AspNetCore.Server.Kestrel.Core.Tests: 9913 passed, 1 skipped.InMemory.FunctionalTests: 1661 passed, 5 skipped, 1 failed. The failure,HttpsConnectionMiddlewareTests.HandshakeExceptionIsAvailableAfterHandshakeFailure, also fails without this change on the same machine.Interop.FunctionalTestsHTTP/2 and h2spec subset: 376 passed, 1 skipped, including all 292 h2spec cases.SocketsHttpHandler, Release builds): unpatched, 24 of 240,000 requests never got response headers (2 runs of 120,000); patched, 0 of 360,000 (3 runs).Servicing
This regressed in 7.0 with #40925; the original report on #53736 notes it started after moving from 6.0. The same code is in
release/8.0,release/9.0,release/10.0, andrelease/11.0, and this patch applies to each. I'd suggest backporting it to 10.0 and 8.0, plus 9.0 and 11.0 per the servicing policy.Thanks to @noocyte and @Misiu for the analysis and repro in #53736.
Fixes #53736