From ffba7f5f041c6b5b6f3fe7633bc156f8d907b184 Mon Sep 17 00:00:00 2001 From: kafkiansky Date: Sun, 11 Oct 2026 19:13:42 +0300 Subject: [PATCH 1/3] Close HTTP/2 socket on shutdown without active streams Http2ConnectionProcessor::shutdown() queues GOAWAY and releases the active streams, but the socket is closed only in releaseStream() once the last stream is released. Closing a connection without active streams, e.g. an idle one, therefore only sent GOAWAY and left the socket open until the server closed it, which some servers (grpc-go) never do. The pool also kept waiting for such a connection to close, so the next request to the same authority could hang. Close the socket once the GOAWAY frame has been written. --- .../Internal/Http2ConnectionProcessor.php | 6 +++++- test/Connection/Http2ConnectionTest.php | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Connection/Internal/Http2ConnectionProcessor.php b/src/Connection/Internal/Http2ConnectionProcessor.php index 71fe070e..727426c9 100644 --- a/src/Connection/Internal/Http2ConnectionProcessor.php +++ b/src/Connection/Internal/Http2ConnectionProcessor.php @@ -1480,7 +1480,11 @@ private function shutdown(?HttpException $reason = null, ?int $lastId = null): v default => null, }; - $this->writeFrame(Http2Parser::GOAWAY, data: \pack('NN', 0, $code) . $message)->ignore(); + // Without active streams nothing else closes the socket, see releaseStream(). Closing it right away + // would discard the GOAWAY frame still waiting in the write queue. + $this->writeFrame(Http2Parser::GOAWAY, data: \pack('NN', 0, $code) . $message) + ->finally($this->socket->close(...)) + ->ignore(); foreach ($this->streams as $id => $_stream) { $reason ??= $this->makeDefaultShutdownReason(); diff --git a/test/Connection/Http2ConnectionTest.php b/test/Connection/Http2ConnectionTest.php index 3f4ff6cd..10a751de 100644 --- a/test/Connection/Http2ConnectionTest.php +++ b/test/Connection/Http2ConnectionTest.php @@ -591,6 +591,22 @@ public function testServerGoAwayFrame(): void } } + public function testCloseWithoutStreamsClosesSocket(): void + { + $this->connection->close(); + + $received = ''; + while (null !== $chunk = $this->server->read(new TimeoutCancellation(1))) { + $received .= $chunk; + } + + self::assertTrue($this->client->isClosed()); + self::assertStringEndsWith( + self::packFrame(\pack('NN', 0, Http2Parser::GRACEFUL_SHUTDOWN), Http2Parser::GOAWAY), + $received, + ); + } + public function testServerEarlyResponse(): void { $request = new Request('http://localhost/', 'POST', BufferedContent::fromString(\str_repeat('a', 2 ** 10))); From c738131d52d3bc70aa6e2f9f660c6dfc771358eb Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Sun, 8 Nov 2026 19:26:22 -0600 Subject: [PATCH 2/3] Close socket in write fiber --- .../Internal/Http2ConnectionProcessor.php | 17 ++++++++-- test/Connection/Http2ConnectionTest.php | 31 +++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/Connection/Internal/Http2ConnectionProcessor.php b/src/Connection/Internal/Http2ConnectionProcessor.php index 62c4933c..6cc37722 100644 --- a/src/Connection/Internal/Http2ConnectionProcessor.php +++ b/src/Connection/Internal/Http2ConnectionProcessor.php @@ -1142,7 +1142,9 @@ private function runReadFiber(): void } } - $this->frameQueue->complete(); + if (!$this->frameQueue->isComplete()) { + $this->frameQueue->complete(); + } $this->shutdown(); } @@ -1402,7 +1404,10 @@ private function releaseStream(int $streamId, ?\Throwable $exception, bool $unpr } if ($this->shutdown !== null) { - $this->socket->close(); + if (!$this->frameQueue->isComplete()) { + $this->frameQueue->complete(); + } + return; } @@ -1484,7 +1489,11 @@ private function shutdown(?HttpException $reason = null, ?int $lastId = null): v // Without active streams nothing else closes the socket, see releaseStream(). Closing it right away // would discard the GOAWAY frame still waiting in the write queue. $this->writeFrame(Http2Parser::GOAWAY, data: \pack('NN', 0, $code) . $message) - ->finally($this->socket->close(...)) + ->finally(function (): void { + if (!$this->frameQueue->isComplete()) { + $this->frameQueue->complete(); + } + }) ->ignore(); foreach ($this->streams as $id => $_stream) { @@ -1647,6 +1656,8 @@ private function runWriteFiber(): void Http2Parser::INTERNAL_ERROR, $exception, )); + } finally { + $this->socket->close(); } } diff --git a/test/Connection/Http2ConnectionTest.php b/test/Connection/Http2ConnectionTest.php index 10a751de..0a931ccf 100644 --- a/test/Connection/Http2ConnectionTest.php +++ b/test/Connection/Http2ConnectionTest.php @@ -607,6 +607,37 @@ public function testCloseWithoutStreamsClosesSocket(): void ); } + public function testWriteErrorWithActiveStreamClosesSocket(): void + { + $request = new Request('http://localhost/'); + + events()->requestStart($request); + + $stream = $this->connection->getStream($request); + + $future = async(fn () => $stream->request($request, new NullCancellation)); + + delay(0.1); + + // Fail writes from the client while leaving its read side open. + \stream_socket_shutdown($this->client->getResource(), \STREAM_SHUT_WR); + + // Writing the PING ACK fails. + $this->server->write(self::packFrame('12345678', Http2Parser::PING)); + + try { + $future->await(new TimeoutCancellation(1)); + self::fail('The request should have failed'); + } catch (SocketException) { + // Expected + } + + delay(0.1); + + self::assertTrue($this->connection->isClosed()); + self::assertTrue($this->client->isClosed()); + } + public function testServerEarlyResponse(): void { $request = new Request('http://localhost/', 'POST', BufferedContent::fromString(\str_repeat('a', 2 ** 10))); From 9fd7c6bb63be57eafb3a728ee9be6e1dea5a6894 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Sun, 8 Nov 2026 19:37:59 -0600 Subject: [PATCH 3/3] Private method for closing queue --- .../Internal/Http2ConnectionProcessor.php | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/Connection/Internal/Http2ConnectionProcessor.php b/src/Connection/Internal/Http2ConnectionProcessor.php index 6cc37722..5c62d53a 100644 --- a/src/Connection/Internal/Http2ConnectionProcessor.php +++ b/src/Connection/Internal/Http2ConnectionProcessor.php @@ -1142,10 +1142,7 @@ private function runReadFiber(): void } } - if (!$this->frameQueue->isComplete()) { - $this->frameQueue->complete(); - } - + $this->completeFrameQueue(); $this->shutdown(); } } @@ -1404,10 +1401,7 @@ private function releaseStream(int $streamId, ?\Throwable $exception, bool $unpr } if ($this->shutdown !== null) { - if (!$this->frameQueue->isComplete()) { - $this->frameQueue->complete(); - } - + $this->completeFrameQueue(); return; } @@ -1489,11 +1483,7 @@ private function shutdown(?HttpException $reason = null, ?int $lastId = null): v // Without active streams nothing else closes the socket, see releaseStream(). Closing it right away // would discard the GOAWAY frame still waiting in the write queue. $this->writeFrame(Http2Parser::GOAWAY, data: \pack('NN', 0, $code) . $message) - ->finally(function (): void { - if (!$this->frameQueue->isComplete()) { - $this->frameQueue->complete(); - } - }) + ->finally($this->completeFrameQueue(...)) ->ignore(); foreach ($this->streams as $id => $_stream) { @@ -1661,6 +1651,13 @@ private function runWriteFiber(): void } } + private function completeFrameQueue(): void + { + if (!$this->frameQueue->isComplete()) { + $this->frameQueue->complete(); + } + } + private function wrapException(\Throwable $exception, ?string $prefix = null): \Throwable { if ($exception instanceof HttpException || $exception instanceof CancelledException) {