From 1e6c0879d76e661941e13bbd6f5a61ae3815747c Mon Sep 17 00:00:00 2001 From: lav45 Date: Mon, 2 Nov 2026 16:11:25 +0300 Subject: [PATCH] Respect cancellation while waiting for a connection in ConnectionLimitingPool 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. --- src/Connection/ConnectionLimitingPool.php | 12 ++-- .../Connection/ConnectionLimitingPoolTest.php | 60 +++++++++++++++++++ 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/src/Connection/ConnectionLimitingPool.php b/src/Connection/ConnectionLimitingPool.php index 1bc38ea7..66f3bbcb 100644 --- a/src/Connection/ConnectionLimitingPool.php +++ b/src/Connection/ConnectionLimitingPool.php @@ -159,13 +159,15 @@ private function getStreamFor(string $uri, Request $request, Cancellation $cance try { if ($isHttps && ($this->waitForPriorConnection[$uri] ?? true)) { // Wait for first successful connection if using a secure connection (maybe we can use HTTP/2). - $connection = $connectionFuture->await(); + $connection = $connectionFuture->await($cancellation); } elseif ($connectionFuture->isComplete()) { - $connection = $connectionFuture->await(); + $connection = $connectionFuture->await($cancellation); } else { continue; } } catch (\Exception $exception) { + $cancellation->throwIfRequested(); + continue; // Ignore cancellations and errors of other requests. } @@ -195,7 +197,7 @@ private function getStreamFor(string $uri, Request $request, Cancellation $cance break; } - $connection = $futureFromDeferred->await(); + $connection = $futureFromDeferred->await($cancellation); \assert($connection instanceof Connection); @@ -248,7 +250,7 @@ private function getStreamFor(string $uri, Request $request, Cancellation $cance try { // Await both new connection future and deferred to reuse an existing connection. - $connection = Future\awaitFirst([$connectionFuture, $futureFromDeferred]); + $connection = Future\awaitFirst([$connectionFuture, $futureFromDeferred], $cancellation); } catch (CompositeException $exception) { [$exception] = $exception->getReasons(); // The first reason is why the connection failed. throw $exception; @@ -262,7 +264,7 @@ private function getStreamFor(string $uri, Request $request, Cancellation $cance if ($stream === null) { // Potentially reused connection did not have an available stream for the given request. - $connection = $connectionFuture->await(); // Wait for new connection request instead. + $connection = $connectionFuture->await($cancellation); // Wait for new connection request instead. $stream = $this->getStreamFromConnection($connection, $request); diff --git a/test/Connection/ConnectionLimitingPoolTest.php b/test/Connection/ConnectionLimitingPoolTest.php index 9a119e79..78f4b4af 100644 --- a/test/Connection/ConnectionLimitingPoolTest.php +++ b/test/Connection/ConnectionLimitingPoolTest.php @@ -3,14 +3,19 @@ namespace Amp\Http\Client\Connection; use Amp\ByteStream\ReadableBuffer; +use Amp\Cancellation; +use Amp\CancelledException; +use Amp\DeferredFuture; use Amp\Future; use Amp\Http\Client\HttpClientBuilder; use Amp\Http\Client\Request; use Amp\Http\Client\Response; use Amp\Http\Client\SocketException; use Amp\Http\Client\Trailers; +use Amp\NullCancellation; use Amp\PHPUnit\AsyncTestCase; use Amp\Socket\InternetAddress; +use Amp\TimeoutCancellation; use PHPUnit\Framework\MockObject\MockObject; use Revolt\EventLoop; use function Amp\async; @@ -18,6 +23,61 @@ class ConnectionLimitingPoolTest extends AsyncTestCase { + public function testCancellationWhileWaitingForConnectionOfAnotherRequest(): void + { + $this->setTimeout(1); + + $deferred = new DeferredFuture(); + + $factory = new class($deferred) implements ConnectionFactory { + public function __construct(private readonly DeferredFuture $deferred) + { + } + + public function create(Request $request, Cancellation $cancellation): Connection + { + return $this->deferred->getFuture()->await(); + } + }; + + $pool = ConnectionLimitingPool::byAuthority(\PHP_INT_MAX, $factory); + + $first = async($pool->getStream(...), new Request('https://example.com/'), new NullCancellation()); + $first->ignore(); + + delay(0); + + $keepLoopAlive = EventLoop::delay(0.5, static fn () => null); + + try { + $this->expectException(CancelledException::class); + + $pool->getStream(new Request('https://example.com/'), new TimeoutCancellation(0.05)); + } finally { + EventLoop::cancel($keepLoopAlive); + $deferred->error(new SocketException('Connection failed')); + } + } + + public function testEstablishedConnectionIsCountedAsOpen(): void + { + $request = new Request('http://localhost'); + + $factory = $this->createMock(ConnectionFactory::class); + $factory->expects(self::once()) + ->method('create') + ->willReturn($this->createMockConnection($request)); + + $pool = ConnectionLimitingPool::byAuthority(1, $factory); + + $pool->getStream($request, new NullCancellation()); + + delay(0); + + self::assertSame(1, $pool->getTotalConnectionAttempts()); + self::assertSame(1, $pool->getOpenConnectionCount()); + } + public function testSingleConnection(): void { $client = (new HttpClientBuilder)