Skip to content

DefaultAsyncClientConnectionOperator: close the session when cancelled during the TLS handshake - #950

Open
shinseongsu wants to merge 1 commit into
apache:masterfrom
shinseongsu:tls-handshake-cancel
Open

shinseongsu wants to merge 1 commit into
apache:masterfrom
shinseongsu:tls-handshake-cancel

Conversation

@shinseongsu

@shinseongsu shinseongsu commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #943.

Once the socket is connected, the future returned by connect() still has the session request as its dependency, and that request is already done. So cancelling during the TLS handshake doesn't touch the session: it stays open until the handshake times out. If the handshake finishes after the cancel, future.completed() is ignored and the connection is left open with nothing holding it. It isn't in the pool and doesn't show up in the pool stats.

The change registers a dependency that closes the connection right before the handshake starts. The session request goes through a ComplexCancellable holder, like in #943, so a connect that completes before sessionRequester.connect() returns can't overwrite it.

How I tested it

Pooling async client, max 20 per route, 20 s connect timeout, against a local TLS server that accepts the TCP connection and then holds the handshake. 20 requests, each cancelled after 300 ms. Client sockets counted with lsof 3 s after the last cancel (the count includes one probe request).

server handshake master patched
never answers 21 ESTABLISHED 1
answers after 1 s 21 ESTABLISHED, pool stats show 0 leased / 0 available 1

On master the server sees the cancelled connections close only when the client is shut down. With the change each one is reset about 0.3 s after it was accepted.

I also ran it under load, 150 s per build: 10 threads, every request cancelled if it takes more than 300 ms, one request per connection, and the server delays the handshake by 2 s on about 10% of new connections. Prometheus scrapes every 2 s.

grafana

On master the client's open fds and the server's open connections grow by about 11 per second and end at ~1.7K, while the pool never sees more than 10 connections. With the change both stay flat (66 fds, 5 server connections). Throughput is the same, so the leaked sockets are pure overhead here, but in a long-running client they'd run into the fd limit eventually.

Single request, server holds the handshake for 1 s, cancel after 300 ms:

  • master: nothing goes out at 0.3 s. The handshake completes at 1.05 s and the connection stays open until the client is closed (FIN at 4.36 s)

master

  • patched: RST at 0.30 s

patched

DefaultAsyncClientConnectionOperatorTest#testCancelDuringTlsHandshakeClosesSession fails on master and passes with the change. httpclient5 verify: 1161 tests, 0 failures. httpclient5-testing async tests: 407, 0 failures.

The connection is closed with CloseMode.IMMEDIATE, so the server gets an RST rather than a close_notify. That seemed right for a handshake that never finished, but let me know if you'd rather have GRACEFUL here.

…d during the TLS handshake

Once the socket is connected, the returned future still pointed at the
session request, which is already done. Cancelling it during the TLS
handshake did nothing: the session stayed open until the handshake
timed out, and if the handshake then succeeded the connection was left
open outside the pool.

Register a dependency that closes the connection before starting the
handshake, and register the session request through a holder so it
cannot replace that dependency when the connect completes early.
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.

1 participant