Fix WebSocket close single-consumer violation #127183
Open
cittaz wants to merge 1 commit intodotnet:mainfrom
Open
Fix WebSocket close single-consumer violation #127183cittaz wants to merge 1 commit intodotnet:mainfrom
cittaz wants to merge 1 commit intodotnet:mainfrom
Conversation
When a user has a pending ReceiveAsync and calls CloseAsync over an
HTTP/2 WebSocket (RFC 8441), two code paths could both reach
WaitForServerToCloseConnectionAsync and each issue a ReadAsync on the
underlying Http2Stream:
1. HandleReceivedCloseAsync (line 1122) — inside the receive loop;
holds _receiveMutex.
2. SendCloseFrameAsync (line 1566) — after sending the close frame;
does not hold _receiveMutex.
Both paths gate on _sentCloseFrame and _receivedCloseFrame respectively.
In rare concurrent Close + Receive scenarios the two flags are set
near-simultaneously, both checks pass, and both paths try to read from
the stream. Http2Stream enforces a single-consumer invariant and trips
Debug.Assert(!_hasWaiter), crashing the test process.
The scenario is explicitly listed as supported in the class-level
thread-safety contract:
/// - It's acceptable to have a pending ReceiveAsync while
/// CloseOutputAsync or CloseAsync is called.
Guard WaitForServerToCloseConnectionAsync with an Interlocked flag so
that only the first caller performs the wait; the other is a no-op.
This preserves RFC 6455 section 7.1.1 behavior (the wait still runs
exactly once), introduces no lock acquisition, and has no reentrancy
or lock-order implications.
Also clarify the long-stale comment in the test that exercises this
contract — the original attributed the OperationCanceledException path
to CloseAsync "receiving" the close frame, which is imprecise. The
exception originates from ReceiveAsyncPrivate's catch block when the
socket becomes Aborted during the close handshake (e.g. a 1s timeout
in WaitForServerToCloseConnectionAsync triggers Abort).
Contributor
|
Tagging subscribers to this area: @karelz, @dotnet/ncl |
This was referenced Apr 21, 2026
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.
Summary
Fixes #121157 (and the likely related #117267 tracking the same assertion).
Adds a CAS guard so
WaitForServerToCloseConnectionAsyncruns at most once per WebSocket, eliminating a race that causesHttp2Stream.TryReadFromBuffer'sDebug.Assert(!_hasWaiter)to fire under concurrentCloseAsync+ pendingReceiveAsyncscenarios over HTTP/2 (RFC 8441).Root cause
WaitForServerToCloseConnectionAsynchas two call sites, both issuing_stream.ReadAsyncfor the RFC 6455 §7.1.1 "wait for server TCP close" step:HandleReceivedCloseAsync(line 1122) — inside the receive loop; holds_receiveMutex. Gated on!_isServer && _sentCloseFrame.SendCloseFrameAsync(line 1566) — after sending the close frame; does not hold_receiveMutex. Gated on!_isServer && _receivedCloseFrame.When a user has a pending
ReceiveAsyncand concurrently callsCloseAsync, the two flags flip near-simultaneously. Both gates pass, both paths callWaitForServerToCloseConnectionAsync, both issue a read on the underlying stream.Over HTTP/1.1 the behaviour is merely wrong (two consumers racing for the same socket bytes) and historically was tolerated by the error-handling paths. Over HTTP/2 (
Http2Stream.Http2ReadWriteStream), the stream enforces a single-consumer invariant that tripsDebug.Assert(!_hasWaiter)inTryReadFromBuffer, crashing the test process.The scenario is explicitly listed as supported in the class-level thread-safety contract:
Fix
Guard
WaitForServerToCloseConnectionAsyncat its top with an interlocked compare-exchange on a new_waitedForServerCloseflag. The first caller flips the flag and runs the wait; any subsequent caller sees the flag set and returns immediately.Behavior preserved
Test-side comment clarification
The test that exercises this contract —
RunClient_CloseAsync_DuringConcurrentReceiveAsync_ExpectedStatesinCloseTest.cs— had a long-stale comment that misattributed whereOperationCanceledExceptioncomes from in the abort branch. Updated the comment to accurately describe the outcomes and where the exception originates (ReceiveAsyncPrivate's catch block translates a stream exception toOperationCanceledExceptionwhen_state == Aborted).No test logic is changed.
Note for reviewers about work item visibility
I'm a community contributor and do not have access to the work-item logs for #121157 / #117267. I tried to identify the failing test, the race condition, and the fix by reading the code:
WaitForServerToCloseConnectionAsyncrunning underHttp2Stream.CloseAsync_DuringConcurrentReceiveAsync_ExpectedStatesunder theCloseTest_*_Http2Loopbackclasses matches exactly.If a reviewer can confirm from the actual work-item logs that this test is the one triggering the assertion, I'll reference the run in this PR.
Local verification
./build.cmd -subset libs -c Release): clean.System.Net.WebSockets.Client.Testsbuild: clean.CloseTest_Invoker_Http2Loopback.CloseAsync_DuringConcurrentReceiveAsync_ExpectedStates(bothuseSslvalues)CloseTest_HttpClient_Http2Loopback.CloseAsync_DuringConcurrentReceiveAsync_ExpectedStates(bothuseSslvalues)CloseTest*regression run (no new failures)