diff --git a/src/Connection/Internal/Http2ConnectionProcessor.php b/src/Connection/Internal/Http2ConnectionProcessor.php index aa29e7eb..5c62d53a 100644 --- a/src/Connection/Internal/Http2ConnectionProcessor.php +++ b/src/Connection/Internal/Http2ConnectionProcessor.php @@ -1142,8 +1142,7 @@ private function runReadFiber(): void } } - $this->frameQueue->complete(); - + $this->completeFrameQueue(); $this->shutdown(); } } @@ -1402,7 +1401,7 @@ private function releaseStream(int $streamId, ?\Throwable $exception, bool $unpr } if ($this->shutdown !== null) { - $this->socket->close(); + $this->completeFrameQueue(); return; } @@ -1481,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->completeFrameQueue(...)) + ->ignore(); foreach ($this->streams as $id => $_stream) { $reason ??= $this->makeDefaultShutdownReason(); @@ -1643,6 +1646,15 @@ private function runWriteFiber(): void Http2Parser::INTERNAL_ERROR, $exception, )); + } finally { + $this->socket->close(); + } + } + + private function completeFrameQueue(): void + { + if (!$this->frameQueue->isComplete()) { + $this->frameQueue->complete(); } } diff --git a/test/Connection/Http2ConnectionTest.php b/test/Connection/Http2ConnectionTest.php index 3f4ff6cd..0a931ccf 100644 --- a/test/Connection/Http2ConnectionTest.php +++ b/test/Connection/Http2ConnectionTest.php @@ -591,6 +591,53 @@ 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 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)));