Skip to content

Fix flaky MaxResponseHeadersLength tests by handling additional SocketError codes#124517

Merged
MihaZupan merged 3 commits intomainfrom
copilot/fix-threshold-exceeded-exception
Feb 17, 2026
Merged

Fix flaky MaxResponseHeadersLength tests by handling additional SocketError codes#124517
MihaZupan merged 3 commits intomainfrom
copilot/fix-threshold-exceeded-exception

Conversation

Copy link
Contributor

Copilot AI commented Feb 17, 2026

Description

The ThresholdExceeded_ThrowsException and LargeSingleHeader_ThrowsException tests were flaking when the client forcibly closed the connection after detecting excessive response headers, but before the server finished writing. The server-side loopback only handled SocketError.Shutdown, missing ConnectionReset and ConnectionAborted which manifest on different platforms/timings for the same race condition.

Changes

Extended exception filters in both test methods to catch all three expected SocketError codes:

// Before
catch (IOException ex) when (ex.InnerException is SocketException se && se.SocketErrorCode == SocketError.Shutdown) { }

// After  
catch (IOException ex) when (ex.InnerException is SocketException se && (se.SocketErrorCode == SocketError.Shutdown || se.SocketErrorCode == SocketError.ConnectionReset || se.SocketErrorCode == SocketError.ConnectionAborted)) { }

This aligns with the existing pattern in LoopbackProxyServer.cs which already handles both ConnectionReset and ConnectionAborted together.

Original prompt

This section details on the original issue you should resolve

<issue_title>[HTTP] ThresholdExceeded_ThrowsException</issue_title>
<issue_description>## Build Information
Build: https://dev.azure.com/dnceng-public/cbb18261-c48f-4abb-8651-8cdcb5474649/_build/results?buildId=1296343
Build error leg or test failing: System.Net.Http.Functional.Tests.WorkItemExecution
Pull request: #124469

Error Message

Fill the error message using step by step known issues guidance.

{
  "ErrorMessage": "",
  "ErrorPattern": "ThresholdExceeded_ThrowsException.*\[FAIL\]",
  "BuildRetry": false,
  "ExcludeConsoleLog": false
}

</issue_description>

<agent_instructions>The exception thrown is

System.IO.IOException : Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host..
---- System.Net.Sockets.SocketException : An existing connection was forcibly closed by the remote host.
   at System.Net.Test.Common.LoopbackServer.Connection.SendResponseAsync(Byte[] response) in /_/src/libraries/Common/tests/System/Net/Http/LoopbackServer.cs:line 762
   at System.Net.Test.Common.LoopbackServer.Connection.SendResponseBodyAsync(Byte[] content, Boolean isFinal) in /_/src/libraries/Common/tests/System/Net/Http/LoopbackServer.cs:line 1018
   at System.Net.Test.Common.GenericLoopbackConnection.SendResponseBodyAsync(String content, Boolean isFinal) in /_/src/libraries/Common/tests/System/Net/Http/GenericLoopbackServer.cs:line 172
   at System.Net.Test.Common.LoopbackServer.Connection.SendResponseAsync(HttpStatusCode statusCode, IList`1 headers, String content, Boolean isFinal) in /_/src/libraries/Common/tests/System/Net/Http/LoopbackServer.cs:line 966
   at System.Net.Http.Functional.Tests.HttpClientHandler_MaxResponseHeadersLength_Test.<>c__DisplayClass6_0.<ThresholdExceeded_ThrowsException>b__1(GenericLoopbackServer server)

Fix the test to account for this being a possibility</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…and ConnectionAborted

Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
@dotnet-policy-service
Copy link
Contributor

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

Copy link
Member

@ManickaP ManickaP left a comment

Choose a reason for hiding this comment

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

LGTM

@MihaZupan MihaZupan marked this pull request as ready for review February 17, 2026 15:57
Copilot AI review requested due to automatic review settings February 17, 2026 15:57
@MihaZupan MihaZupan enabled auto-merge (squash) February 17, 2026 15:57
Copilot AI changed the title [WIP] Fix threshold exceeded exception in HTTP tests Fix flaky MaxResponseHeadersLength tests by handling additional SocketError codes Feb 17, 2026
Copilot AI requested a review from MihaZupan February 17, 2026 16:15
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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@MihaZupan MihaZupan merged commit e02e618 into main Feb 17, 2026
93 of 97 checks passed
@MihaZupan MihaZupan deleted the copilot/fix-threshold-exceeded-exception branch February 17, 2026 19:19
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.

[HTTP] ThresholdExceeded_ThrowsException

3 participants