Skip to content

MinimalHttpAsyncClient: keep the connect cancellable when the lease completes synchronously - #951

Merged
ok2c merged 1 commit into
apache:masterfrom
shinseongsu:minimal-lease-cancel
Nov 18, 2026
Merged

ok2c merged 1 commit into
apache:masterfrom
shinseongsu:minimal-lease-cancel

Conversation

@shinseongsu

@shinseongsu shinseongsu commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #943, the same overwrite in MinimalHttpAsyncClient#leaseEndpoint.

When the pool has room, manager.lease() completes synchronously. The lease callback starts the connect and calls resultFuture.setDependency(connectFuture) before lease() returns, and the resultFuture.setDependency(leaseFuture) right after it replaces that with the lease, which is already done. Cancelling the request during the connect doesn't reach the connect. When the connect finishes, resultFuture.completed() is ignored because the future is already cancelled, so the endpoint is never released. Every such cancel takes one connection out of the pool for good.

The change uses the same ComplexCancellable holder as #943.

How I tested it

HttpAsyncClients.createMinimal(cm), max 20 per route, against a local TLS server that finishes the handshake after 1 s. 20 requests, each cancelled after 300 ms, then one more request with a 2 s connection request timeout.

master patched
pool 3 s after the last cancel leased: 20, available: 0 leased: 0
next request no response within 10 s 200 in 1.06 s

With this change alone the cancelled TLS sessions still stay open until their handshake ends. That part is #950.

Same thing 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 all 20 connections are leased from the first samples and stay that way. Only 196 requests succeed in 150 s; the rest wait for a lease until they're cancelled. With the change it's 15.2K successful requests (~97 req/s) and no pending leases. The client's open sockets still grow on this branch, which is the handshake issue fixed in #950.

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

While testing I also noticed that cancelling a MinimalHttpAsyncClient request after the exchange has started doesn't abort it. execute() only links the cancellable to the lease future, which is complete by then, so the server still sends the response 3 s later and the connection goes back to the pool. HttpAsyncClient closes the connection in the same case. Is that intended for the minimal client, or should I look into it?

…ompletes synchronously

When the pool hands out a new endpoint right away, the lease callback
starts the connect and registers it with the result future before
lease() returns. The lease future, which is already done, then replaced
it, so cancelling the request did not reach the connect. When the
connect later finished, the result future was already cancelled and the
endpoint was never released back to the pool.

Register the lease through a holder installed before the lease starts.
@ok2c
ok2c merged commit 168c9b5 into apache:master Nov 18, 2026
12 checks passed
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