Skip to content

[HTTP] Fix HttpListener tests blocking xunit workers (#21870)#124469

Merged
ManickaP merged 1 commit intodotnet:mainfrom
ManickaP:fix/httplistener-test-parallelism-21870
Feb 17, 2026
Merged

[HTTP] Fix HttpListener tests blocking xunit workers (#21870)#124469
ManickaP merged 1 commit intodotnet:mainfrom
ManickaP:fix/httplistener-test-parallelism-21870

Conversation

@ManickaP
Copy link
Member

Replace blocking patterns that cause deadlocks when xunit parallel execution is enabled:

  • Replace .Result with await on task completions
  • Replace Task.Run(() => GetContext()) with GetContextAsync()
  • Replace Task.Run(() => Receive()) with ReceiveAsync()
  • Convert synchronous Socket.Send() to SendAsync() in async methods
  • Re-enable parallel test execution by removing DisableTestParallelization and MaxParallelThreads = 1
  • Fix disposal order in HttpListenerRequestTests to close the client socket before the listener, avoiding a 1s linger timeout per test

Fixes #21870

Replace blocking patterns that cause deadlocks when xunit parallel
execution is enabled:

- Replace .Result with await on task completions
- Replace Task.Run(() => GetContext()) with GetContextAsync()
- Replace Task.Run(() => Receive()) with ReceiveAsync()
- Convert synchronous Socket.Send() to SendAsync() in async methods
- Re-enable parallel test execution by removing
  DisableTestParallelization and MaxParallelThreads = 1
- Fix disposal order in HttpListenerRequestTests to close the client
  socket before the listener, avoiding a 1s linger timeout per test

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings February 16, 2026 15:40
@ManickaP ManickaP requested a review from a team February 16, 2026 15:41
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates System.Net.HttpListener tests to avoid blocking xUnit worker threads (e.g., .Result, Task.Run around sync socket APIs), so the suite can run reliably when xUnit parallel execution is enabled.

Changes:

  • Replace blocking wait patterns with await, and replace Task.Run(() => ...) wrappers with native async APIs (GetContextAsync, socket async APIs).
  • Convert synchronous socket sends to SendAsync inside async test flows.
  • Remove the assembly-level DisableTestParallelization/MaxParallelThreads = 1 restriction and adjust disposal ordering to reduce per-test timeouts.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/libraries/System.Net.HttpListener/tests/XUnitAssemblyAttributes.cs Removes forced disabling of parallelization at the assembly level.
src/libraries/System.Net.HttpListener/tests/InvalidClientRequestTests.cs Switches send/receive to async and avoids .Result (but currently has a Task/ValueTask mismatch).
src/libraries/System.Net.HttpListener/tests/HttpResponseStreamTests.cs Uses SendAsync instead of blocking Send in async tests.
src/libraries/System.Net.HttpListener/tests/HttpRequestStreamTests.cs Uses SendAsync instead of blocking Send in async tests.
src/libraries/System.Net.HttpListener/tests/HttpListenerResponseTests.cs Uses SendAsync instead of blocking Send when establishing contexts.
src/libraries/System.Net.HttpListener/tests/HttpListenerRequestTests.cs Fixes disposal order and replaces .Result with await; uses SendAsync in request helper.
src/libraries/System.Net.HttpListener/tests/HttpListenerContextTests.cs Uses SendAsync instead of blocking Send when establishing contexts.
src/libraries/System.Net.HttpListener/tests/HttpListenerAuthenticationTests.cs Removes Task.Run(() => GetContext()) + cancellation token pattern; uses GetContextAsync and await for client responses.

Copy link
Member

@wfurt wfurt left a comment

Choose a reason for hiding this comment

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

LGTM

@ManickaP
Copy link
Member Author

/ba-g #124515

@ManickaP ManickaP merged commit 35ad59d into dotnet:main Feb 17, 2026
95 of 98 checks passed
@ManickaP ManickaP deleted the fix/httplistener-test-parallelism-21870 branch February 17, 2026 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[HttpListener] HttpListener tests are blocking xunit workers, causing hangs

2 participants