Skip to content
Open
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
24 changes: 22 additions & 2 deletions src/Drivers/LaravelHttpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Amp\Http\Server\RequestHandler\ClosureRequestHandler;
use Amp\Http\Server\Response;
use Amp\Http\Server\SocketHttpServer;
use Amp\Socket\InternetAddress;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Contracts\Http\Kernel as HttpKernel;
use Illuminate\Foundation\Testing\Concerns\WithoutExceptionHandlingHandler;
Expand Down Expand Up @@ -50,6 +51,15 @@ final class LaravelHttpServer implements HttpServer
*/
private ?Throwable $lastThrowable = null;

/**
* The port the server actually bound to, resolved at start time.
*
* When constructed with port 0 the operating system assigns a free
* ephemeral port atomically at bind time, which avoids the find-then-rebind
* race that can throw "Address already in use" under parallel test runs.
*/
private ?int $resolvedPort = null;

/**
* Creates a new laravel http server instance.
*/
Expand Down Expand Up @@ -106,6 +116,16 @@ public function start(): void
new ClosureRequestHandler($this->handleRequest(...)),
new DefaultErrorHandler(),
);

foreach ($server->getServers() as $socketServer) {
$address = $socketServer->getAddress();

if ($address instanceof InternetAddress) {
$this->resolvedPort = $address->getPort();

break;
}
}
}

/**
Expand Down Expand Up @@ -202,7 +222,7 @@ private function url(): string
throw new ServerNotFoundException('The HTTP server is not running.');
}

return sprintf('http://%s:%d', $this->host, $this->port);
return sprintf('http://%s:%d', $this->host, $this->resolvedPort ?? $this->port);
}

/**
Expand Down Expand Up @@ -264,7 +284,7 @@ private function handleRequest(AmpRequest $request): Response
// Set the Host header to match the configured host for subdomain routing
$configuredHost = Playwright::host();
if ($configuredHost !== null) {
$hostHeader = sprintf('%s:%d', $configuredHost, $this->port);
$hostHeader = sprintf('%s:%d', $configuredHost, $this->resolvedPort ?? $this->port);
$symfonyRequest->headers->set('Host', $hostHeader);
// Also set SERVER_NAME for Laravel routing
$symfonyRequest->server->set('SERVER_NAME', $configuredHost);
Expand Down
4 changes: 3 additions & 1 deletion src/ServerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ public function http(): HttpServer
return $this->http ??= match (function_exists('app_path')) {
true => new LaravelHttpServer(
self::DEFAULT_HOST, // Always bind to 127.0.0.1 for server
Port::find(),
0, // Bind to an OS-assigned ephemeral port at bind time; the server
// resolves the actual port after starting. Avoids the find-then-rebind
// race that throws "Address already in use" under parallel runs.
),
default => new NullableHttpServer(),
};
Expand Down