-
-
Notifications
You must be signed in to change notification settings - Fork 166
Description
When a program using the library ends unexpectedly or the loop is stopped, the ServerInstance socket might not close the port. This results in the port remaining open until the process times out. If the program restarts suddenly and didn't properly close the socket, it will be unable to reopen the port.
To Reproduce
- Create a simple ReactPHP HttpServer instance.
- Start the server and listen on a specific port.
- Terminate the program unexpectedly (e.g., force kill the process).
- Attempt to restart the program and listen on the same port.
Expected behavior
The socket should close properly, releasing the port for reuse immediately after the program terminates or the loop stops.
Actual behavior
The port remains open and the HttpServer will not start, throwing an error that it couldn't listen to the port.
Example Code
<?php
use React\EventLoop\Factory;
use React\Http\Server;
use React\Socket\Server as SocketServer;
require 'vendor/autoload.php';
$loop = Factory::create();
$server = new Server(function (Psr\Http\Message\ServerRequestInterface $request) {
return new React\Http\Message\Response(
200,
array('Content-Type' => 'text/plain'),
"Hello World!\n"
);
});
$socket = new SocketServer('127.0.0.1:8080', $loop);
$server->listen($socket);
$loop->run();
// Simulate unexpected termination (e.g., force kill the process)This took me a while to figure out, and the best workaround to fixing this odd behavior is to explicitly call $socket->close(); in my error handlers or anywhere that might cause my scripts to restart. I think it would be a good idea to define a __destruct() magic method for this class that makes sure that the socket closes out completely, but I'm not sure if that is the best fix for this bug.