From e18b95ca5fec04f6e687434811875c63d9a53199 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Sun, 15 Nov 2026 09:20:23 -0500 Subject: [PATCH 1/2] fix: consume RPC messages once --- .../JsonRpc/ProcessJsonRpcClient.php | 28 ++++++++++--- .../JsonRpc/ProcessJsonRpcClientTest.php | 39 +++++++++++++++++++ 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/src/Transport/JsonRpc/ProcessJsonRpcClient.php b/src/Transport/JsonRpc/ProcessJsonRpcClient.php index 94ae10f..59dff2f 100644 --- a/src/Transport/JsonRpc/ProcessJsonRpcClient.php +++ b/src/Transport/JsonRpc/ProcessJsonRpcClient.php @@ -39,6 +39,12 @@ final class ProcessJsonRpcClient extends JsonRpcClient implements JsonRpcClientI /** @var array> */ private array $responses = []; + /** @var array */ + private array $waitingRequests = []; + + /** @var \SplQueue */ + private \SplQueue $messages; + private ?\Closure $eventHandler = null; private ?\Throwable $eventHandlerException = null; @@ -55,6 +61,7 @@ public function __construct( defaultTimeoutMs: $defaultTimeoutMs ); + $this->messages = new \SplQueue(); $this->inputStream = $this->processLauncher->getInputStream(); if (!$this->inputStream instanceof InputStream) { throw new NetworkException('ProcessLauncher must have an InputStream for JSON-RPC communication'); @@ -96,9 +103,12 @@ protected function sendAndReceive(array $request, ?float $deadline): array throw new NetworkException('Failed to write to process stdin: '.$e->getMessage(), 0, $e); } + $this->waitingRequests[$requestId] = true; + try { return $this->waitForResponse($requestId, $deadline); } finally { + unset($this->waitingRequests[$requestId], $this->responses[$requestId]); $this->throwEventHandlerException(); } } @@ -149,12 +159,14 @@ private function readProcessOutput(): void private function processAndDispatchMessages(): void { $decoded = LspFraming::decode($this->outputBuffer); - + $this->outputBuffer = $decoded['remainingBuffer']; foreach ($decoded['messages'] as $messageContent) { - $this->dispatchMessage($messageContent); + $this->messages->enqueue($messageContent); } - $this->outputBuffer = $decoded['remainingBuffer']; + while (!$this->messages->isEmpty()) { + $this->dispatchMessage($this->messages->dequeue()); + } } private function dispatchMessage(string $messageContent): void @@ -195,7 +207,10 @@ private function dispatchMessage(string $messageContent): void $typedData[$k] = $v; } } - $this->responses[$data['id']] = $typedData; + + if (isset($this->waitingRequests[$data['id']])) { + $this->responses[$data['id']] = $typedData; + } } elseif (isset($data['requestId']) && (is_int($data['requestId']) || is_string($data['requestId']))) { $this->logger->debug('Received raw response', [ 'requestId' => $data['requestId'], @@ -207,7 +222,10 @@ private function dispatchMessage(string $messageContent): void $typedData[$k] = $v; } } - $this->responses[$data['requestId']] = $typedData; + + if (isset($this->waitingRequests[$data['requestId']])) { + $this->responses[$data['requestId']] = $typedData; + } } if (isset($data['event'])) { diff --git a/tests/Unit/Transport/JsonRpc/ProcessJsonRpcClientTest.php b/tests/Unit/Transport/JsonRpc/ProcessJsonRpcClientTest.php index 8ab194a..8a47ad0 100644 --- a/tests/Unit/Transport/JsonRpc/ProcessJsonRpcClientTest.php +++ b/tests/Unit/Transport/JsonRpc/ProcessJsonRpcClientTest.php @@ -285,4 +285,43 @@ public function testReadProcessOutputClearsBuffersAcrossMultiplePolls(): void $readProcessOutput->invoke($client); $readProcessOutput->invoke($client); } + + public function testNestedRequestsConsumeQueuedEventsExactlyOnce(): void + { + $calls = 0; + $nested = null; + $this->client->setEventHandler(function () use (&$calls, &$nested): void { + ++$calls; + + if (1 === $calls) { + $nested = $this->client->sendRaw(['action' => 'page.title', 'requestId' => 2]); + } + }); + $this->mockProcess->method('getIncrementalOutput')->willReturnOnConsecutiveCalls( + LspFraming::encode('{"event":"console"}').LspFraming::encode('{"event":"response"}').LspFraming::encode('{"requestId":1,"value":"outer"}'), + LspFraming::encode('{"requestId":2,"value":"nested"}'), + '', + ); + + $response = $this->client->sendRaw(['action' => 'page.url', 'requestId' => 1]); + + $this->assertSame(2, $calls); + $this->assertSame('outer', $response['value']); + $this->assertSame('nested', $nested['value'] ?? null); + } + + public function testUnsolicitedRepliesAreNotRetained(): void + { + $this->mockProcess->method('getIncrementalOutput')->willReturn( + LspFraming::encode('{"requestId":"custom_async_id","success":true}') + .LspFraming::encode('{"id":999,"result":{}}') + .LspFraming::encode('{"requestId":1,"value":"ok"}'), + ); + + $response = $this->client->sendRaw(['action' => 'page.url', 'requestId' => 1]); + $responses = new \ReflectionProperty($this->client, 'responses'); + + $this->assertSame('ok', $response['value']); + $this->assertSame([], $responses->getValue($this->client)); + } } From 76abf8e4a556aadf056f3e944c271b966fcf8f99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Andr=C3=A9?= Date: Wed, 18 Nov 2026 00:32:14 +0100 Subject: [PATCH 2/2] Add #219 to the changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 392cfaf..3323ea5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ - A locator assertion on a locator that resolves to several elements fails at once with Playwright's strict mode message instead of waiting for its timeout (#226) - `Frame::waitForFunction()` and `Locator::waitForFunction()` call a function given as a string and wait for its result instead of returning at once (#215) - `Page::waitForFunction()` evaluates an expression in the page, never in the server process (#215) +- Each message from the server reaches its handler once, even when an event handler sends a request, and replies that no request waits for are dropped instead of kept in memory (#219) ## [1.6.0] - 2026-11-07