Skip to content

Fix HttpListener GetContext race with Close on Unix - #136298

Merged
wfurt merged 3 commits into
dotnet:mainfrom
martincostello:gh-136293
Oct 26, 2026
Merged

wfurt merged 3 commits into
dotnet:mainfrom
martincostello:gh-136293

Conversation

@martincostello

Copy link
Copy Markdown
Member

Fix BeginGetContext() and GetContextAsync() never completing in the managed HttpListener implementation if they race with Stop(), Close() or Abort().

_state is only updated after Cleanup() has drained the queue of pending operations, so an operation that passed the state check before the queue was drained could be added to it afterwards, and then nothing would ever complete it. Instead, record that the queue has been drained and complete such operations with an ObjectDisposedException, the same as operations that were already pending.

Also adds a test that calls BeginGetContext() from multiple threads while the listener is terminated.

Fixes #136293.

Fix `BeginGetContext()` and `GetContextAsync()` never completing in the managed `HttpListener` implementation if they race with `Stop()`, `Close()` or `Abort()`.

`_state` is only updated after `Cleanup()` has drained the queue of pending operations, so an operation that passed the state check before the queue was drained could be added to it afterwards, and then nothing would ever complete it. Instead, record that the queue has been drained and complete such operations with an `ObjectDisposedException`, the same as operations that were already pending.

Also adds a test that calls `BeginGetContext()` from multiple threads while the listener is terminated.

Fixes dotnet#136293.

Assisted-by: Claude Code <[email protected]>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 22, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @karelz, @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

@martincostello
martincostello marked this pull request as ready for review October 22, 2026 17:53
Copilot AI balanced review requested due to automatic review settings October 22, 2026 17:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@wfurt wfurt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. thanks for the contribution @martincostello

@wfurt
wfurt merged commit d261849 into dotnet:main Oct 26, 2026
88 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 27, 2026
@martincostello
martincostello deleted the gh-136293 branch October 27, 2026 13:59
@claus-vestergaard

Copy link
Copy Markdown

Thanks for the fix. Is a release/10.0 backport planned? We hit this as an intermittent CI hang on .NET 10.0.15 (a test's loopback server awaiting its accept loop after Close()), and release/10.0 and v10.0.15 don't have _asyncWaitQueueClosed yet.

For what it's worth, a standalone repro on 10.0.15 (Ubuntu 24.04 container, 4 CPUs, 60 s per mode, 200 ms grace, then a last look 5 s after the run):

Mode Listeners Accepts begun Still pending after Close()/Stop() Completed later
one BeginGetContext, no Close() (control) 300 300 300 300, by the final Close()
tight BeginGetContext loop racing Close() 300 246,159 299, on 299 listeners 0
tight BeginGetContext loop racing Stop() 302 290,151 299, on 299 listeners 299, by a later Close()
one GetContextAsync() racing Close() 319 303 started 5 0

The last row is the shape we hit: 5 of 303 single accepts that raced Close() never completed (6 of 302 in an earlier run). An accept that Stop() strands is completed by a later Close(), but a loop awaited between the two hangs. Our workaround is GetContextAsync().WaitAsync(token), with the token cancelled before Close()/Stop().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Net.Http community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HttpListener on Unix: GetContextAsync()/BeginGetContext() racing with Close() never completes

4 participants