Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 23 additions & 5 deletions src/Transport/JsonRpc/ProcessJsonRpcClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ final class ProcessJsonRpcClient extends JsonRpcClient implements JsonRpcClientI
/** @var array<int|string, array<string, mixed>> */
private array $responses = [];

/** @var array<int|string, true> */
private array $waitingRequests = [];

/** @var \SplQueue<string> */
private \SplQueue $messages;

private ?\Closure $eventHandler = null;
private ?\Throwable $eventHandlerException = null;

Expand All @@ -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');
Expand Down Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'],
Expand All @@ -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'])) {
Expand Down
39 changes: 39 additions & 0 deletions tests/Unit/Transport/JsonRpc/ProcessJsonRpcClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Loading