Repository navigation
Fix 100% CPU spin when proxy closes connection during CONNECT - #723
Merged
Merged
Conversation
HttpProxyClient read the CONNECT response one byte at a time until it saw \r\n\r\n but never checked for ReadAsync returning 0. If the proxy accepted the TCP connection and then closed it, the loop spun forever (until cancellation), pegging CPU and stalling the polling client. Now throws a ProxyException so the normal retry/backoff applies.
sburmanoctopus
approved these changes
Oct 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Background
Fixes: LEV-2050
A polling Tentacle configured with an HTTP proxy can get stuck at 100% CPU until the service is restarted (LEV-2050).
HttpProxyClient.SendConnectionCommandAsyncreads the proxy's CONNECT response one byte at a time until it sees\r\n\r\n. It never checked whetherReadAsyncreturned 0, which means the connection was closed. So when a proxy accepted the TCP connection and then closed it before finishing its response, for example while restarting, the loop spun forever. Each pass also calledStringBuilder.ToString(), so it allocated continuously and kept the GC busy too. The read timeout doesn't help, because a read on a closed connection returns 0 straight away instead of waiting. The only way out was cancellation, which in practice meant stopping the service.The loop was introduced in #468.
Change
ReadAsyncreturns 0, throw aProxyException(causedByNetworkError: true). The existing retry and polling backoff then handle it as usual.Tests
HttpProxyClientFixturewith a fake proxy that accepts the connection, reads the CONNECT request, then closes. One case closes without sending anything; the other sends a partial response first.ProxyException.ProxyFixtureand the new fixture pass on net8.0 (macOS): 52 tests.Not covered
TcpClientis never disposed, so each failed attempt leaks a socket. That's separate from the CPU problem and is left for a follow-up.