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 <noreply@anthropic.com>
@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
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

3 participants