Fix ClientWebSocket hang with HttpVersionPolicy.RequestVersionOrHigher by correcting operator precedence #116817
+44
−2
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
This PR fixes an issue where
ClientWebSocket.Connect
hangs indefinitely when usingHttpVersionPolicy.RequestVersionOrHigher
withHttpVersion.Version11
on WSS connections.Root Cause
The issue was in the operator precedence of the conditional logic at lines 80-81 in
WebSocketHandle.Managed.cs
. The current logic:Was being evaluated as:
But should be:
The missing parentheses caused the logic to bypass the
!tryDowngrade
check when the second condition was true, leading to infinite retries of HTTP/2 instead of proper fallback to HTTP/1.1.Reproduction
The issue could be reproduced with:
Fix
Added parentheses to ensure the
!tryDowngrade
condition is properly evaluated for both HTTP/2 and HTTP/1.1 withRequestVersionOrHigher
scenarios.Testing
ConnectAsync_Http11WithRequestVersionOrHigher_Loopback_Success
that validates the fixFixes #111977.
Warning
Firewall rules blocked me from connecting to one or more addresses
I tried to connect to the following addresses, but was blocked by firewall rules:
7b9794c0e7224d04a6e8345cb8e689e5
/home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net10.0-linux-Debug-x64/dotnet exec --runtimeconfig /home/REDACTED/work/runtime/runtime/artifacts/bin/System.Net.WebSockets.Client.Tests/Debug/net10.0/System.Net.WebSockets.Client.Tests.runtimeconfig.json --depsfile /home/REDACTED/work/runtime/runtime/artifacts/bin/System.Net.WebSockets.Client.Tests/Debug/net10.0/System.Net.WebSockets.Client.Tests.deps.json /home/REDACTED/work/runtime/runtime/artifacts/bin/System.Net.WebSockets.Client.Tests/Debug/net10.0/testhost.dll --port 35729 --endpoint 127.0.0.1:035729 --role client --parentprocessid 38138 --datacollectionport 45605 --telemetryoptedin false
(dns block)8345476dd6e44d04be5703ccd90b5152
/home/REDACTED/work/runtime/runtime/artifacts/bin/testhost/net10.0-linux-Debug-x64/dotnet exec --runtimeconfig /home/REDACTED/work/runtime/runtime/artifacts/bin/System.Net.WebSockets.Client.Tests/Debug/net10.0/System.Net.WebSockets.Client.Tests.runtimeconfig.json --depsfile /home/REDACTED/work/runtime/runtime/artifacts/bin/System.Net.WebSockets.Client.Tests/Debug/net10.0/System.Net.WebSockets.Client.Tests.deps.json /home/REDACTED/work/runtime/runtime/artifacts/bin/System.Net.WebSockets.Client.Tests/Debug/net10.0/testhost.dll --port 35729 --endpoint 127.0.0.1:035729 --role client --parentprocessid 38138 --datacollectionport 45605 --telemetryoptedin false
(dns block)9d5c5fafa5974fa294aa489c97acd9ac
echo.websocket.org
/tmp/TestWebSocketFix/bin/Debug/net10.0/TestWebSocketFix
(dns block)If you need me to access, download, or install something from one of these locations, you can either:
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.