Skip to content

Fix 100% CPU spin when proxy closes connection during CONNECT - #723

Merged
LukeButters merged 4 commits into
mainfrom
luke/lev-2050-proxy-connect-eof-spin
Oct 8, 2026
Merged

LukeButters merged 4 commits into
mainfrom
luke/lev-2050-proxy-connect-eof-spin

Conversation

@LukeButters

@LukeButters LukeButters commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

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.SendConnectionCommandAsync reads the proxy's CONNECT response one byte at a time until it sees \r\n\r\n. It never checked whether ReadAsync returned 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 called StringBuilder.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

  • If ReadAsync returns 0, throw a ProxyException (causedByNetworkError: true). The existing retry and polling backoff then handle it as usual.

Tests

  • New HttpProxyClientFixture with 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.
    • Before the fix, both cases spun until the 10s cancellation, using about 92–97s of CPU.
    • After the fix, both fail promptly with a ProxyException.
  • ProxyFixture and the new fixture pass on net8.0 (macOS): 52 tests.

Not covered

  • I have not run this on Windows or net48. The customer was on net48 on Windows.
  • When proxy connection setup fails, the TcpClient is never disposed, so each failed attempt leaks a socket. That's separate from the CPU problem and is left for a follow-up.

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.
@LukeButters
LukeButters requested a review from a team as a code owner October 7, 2026 00:24
@LukeButters
LukeButters merged commit d86fa24 into main Oct 8, 2026
13 checks passed
@LukeButters
LukeButters deleted the luke/lev-2050-proxy-connect-eof-spin branch October 8, 2026 05:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants