From d140ae1928d67faebe20c8f12497f5d5080f7848 Mon Sep 17 00:00:00 2001 From: Andi Wieser Date: Thu, 6 Aug 2026 12:13:16 +0200 Subject: [PATCH] fix: persist the port of the server that was actually started `??=` already makes the Playwright server a one-off, but the two lines around it ran on every call to `playwright()`. So a second call allocated a fresh port that nothing was listening on, and then persisted that port as the description of the server that IS running. Every later `visit()` reads that description, so workers connect to a dead port. Moving the port allocation and the persist inside the guard keeps them with the creation they describe. --- src/ServerManager.php | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/ServerManager.php b/src/ServerManager.php index 4e406fd9..cf102d29 100644 --- a/src/ServerManager.php +++ b/src/ServerManager.php @@ -59,21 +59,31 @@ public function playwright(): PlaywrightServer return AlreadyStartedPlaywrightServer::fromPersisted(); } - $port = Port::find(); - $host = Playwright::host() ?? self::DEFAULT_HOST; + // Allocating the port and persisting the description belong INSIDE the + // guard, with the creation they describe. `??=` already makes the server + // a one-off, but both lines around it ran on every call: the second call + // allocated a fresh port that nothing was listening on, and then + // persisted that port as the description of the server that IS running. + // + // Every later `visit()` reads that description, so the workers connect + // to a dead port. + if (! $this->playwright instanceof PlaywrightServer) { + $port = Port::find(); + $host = Playwright::host() ?? self::DEFAULT_HOST; - $this->playwright ??= PlaywrightNpmServer::create( - PackageJsonDirectory::find(), - '.'.DIRECTORY_SEPARATOR.'node_modules'.DIRECTORY_SEPARATOR.'.bin'.DIRECTORY_SEPARATOR.'playwright run-server --host %s --port %d --mode launchServer', - $host, - $port, - 'Listening on', - ); + $this->playwright = PlaywrightNpmServer::create( + PackageJsonDirectory::find(), + '.'.DIRECTORY_SEPARATOR.'node_modules'.DIRECTORY_SEPARATOR.'.bin'.DIRECTORY_SEPARATOR.'playwright run-server --host %s --port %d --mode launchServer', + $host, + $port, + 'Listening on', + ); - AlreadyStartedPlaywrightServer::persist( - $host, - $port, - ); + AlreadyStartedPlaywrightServer::persist( + $host, + $port, + ); + } return $this->playwright; }