Skip to content

Commit c32186c

Browse files
committed
Do not throw when writing if socket closed
The call to shutdown() in runReadFiber will fail the request if the socket closes. The exception thrown in runWriteFiber was sometimes being caught by the event loop error handler as the top-level exception, not a previous exception as would be expected. I'm not sure how this is possible, but removing the throw fixed the issue.
1 parent 09212eb commit c32186c

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/Connection/Internal/Http2ConnectionProcessor.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ final class Http2ConnectionProcessor implements Http2Processor
100100

101101
private ?int $shutdown = null;
102102

103+
/** @var Queue<string> */
103104
private readonly Queue $frameQueue;
104105

105106
public function __construct(
@@ -1597,11 +1598,16 @@ private function createStreamTimeoutWatcher(int $streamId, float $timeout, strin
15971598
private function runWriteFiber(): void
15981599
{
15991600
try {
1600-
foreach ($this->frameQueue->iterate() as $frame) {
1601+
$iterator = $this->frameQueue->iterate();
1602+
1603+
while ($iterator->continue()) {
16011604
if (!$this->socket->isWritable()) {
1602-
throw new SocketException('Connection has closed');
1605+
$this->hasWriteError = true;
1606+
$iterator->dispose();
1607+
return;
16031608
}
16041609

1610+
$frame = $iterator->getValue();
16051611
$this->socket->write($frame);
16061612
}
16071613
} catch (\Throwable $exception) {
@@ -1610,7 +1616,7 @@ private function runWriteFiber(): void
16101616
$this->shutdown(new SocketException(
16111617
"The HTTP/2 connection closed unexpectedly: " . $exception->getMessage(),
16121618
Http2Parser::INTERNAL_ERROR,
1613-
$exception
1619+
$exception,
16141620
));
16151621
}
16161622
}

0 commit comments

Comments
 (0)