Skip to content

Respect cancellation while waiting for a connection in ConnectionLimitingPool - #406

Merged
trowski merged 2 commits into
amphp:5.xfrom
lav45:pool-await-with-cancellation
Nov 10, 2026
Merged

trowski merged 2 commits into
amphp:5.xfrom
lav45:pool-await-with-cancellation

Conversation

@lav45

@lav45 lav45 commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Problem

On https a request waits for connections that other requests are establishing, because the first one may turn out to be
HTTP/2. That wait, like the others in getStreamFor(), ignores the cancellation of the request.

So a request to a host that can't be reached stays stuck behind everyone else's connection attempts, whatever its
timeout. Sending 500 requests with a 10 s TimeoutCancellation to a host that doesn't resolve, only 2 finished in 40 s.

Solution

Pass $cancellation to the awaits in getStreamFor(), and rethrow it in the catch that ignores failures of other
requests. The background callback in EventLoop::queue() is unchanged: it has to finish regardless of who cancels.

With the change, all 500 requests are cancelled at 10 s, with a peak of 18 MB. A healthy HTTP/2 host still gets 3
connections for 300 concurrent requests, as before.

There are two new tests. testCancellationWhileWaitingForConnectionOfAnotherRequest fails without the fix.
testEstablishedConnectionIsCountedAsOpen covers what no test checked so far: the state of the pool after a connection
is established. An error in that callback only showed up as a lost connection.

lav45 and others added 2 commits November 2, 2026 16:22
…tingPool

A request waiting for the connection of another request ignored its own
cancellation, so a chain of failing connections to one authority couldn't
be left: 2 of 500 requests finished in 40 s, all 500 are cancelled on time
with this change.
@trowski
trowski merged commit 4be3461 into amphp:5.x Nov 10, 2026
8 checks passed
@trowski

trowski commented Nov 10, 2026

Copy link
Copy Markdown
Member

Thanks!

@lav45
lav45 deleted the pool-await-with-cancellation branch November 10, 2026 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants