Skip to content

Commit

Permalink
Stop event loop in master process correctly and set the global static…
Browse files Browse the repository at this point in the history
… driver in EventLoop
  • Loading branch information
luzrain committed Jun 16, 2024
1 parent 0986bda commit f2c2253
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Internal/MasterProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Luzrain\PHPStreamServer\Server;
use Luzrain\PHPStreamServer\WorkerProcess;
use Psr\Log\LoggerInterface;
use Revolt\EventLoop;
use Revolt\EventLoop\Driver;
use Revolt\EventLoop\Driver\StreamSelectDriver;
use Revolt\EventLoop\Suspension;
Expand Down Expand Up @@ -120,6 +121,7 @@ private function initServer(): void
// Init event loop.
// Force use StreamSelectDriver in the master process because it uses pcntl_signal to handle signals, and it works better for this case.
$this->eventLoop = new StreamSelectDriver();
EventLoop::setDriver($this->eventLoop);
$this->eventLoop->setErrorHandler(ErrorHandler::handleException(...));
$this->suspension = $this->eventLoop->getSuspension();

Expand Down Expand Up @@ -220,7 +222,8 @@ private function watchChildProcesses(): void
// Runs in forked process
private function runWorker(WorkerProcess $worker): int
{
$this->eventLoop->stop();
$this->eventLoop->queue(fn() => $this->eventLoop->stop());
$this->eventLoop->run();
\fclose($this->interProcessSocketPair[0]);
unset($this->suspension, $this->eventLoop, $this->pool, $this->processStatusPool, $this->interProcessSocketPair[0]);
\gc_collect_cycles();
Expand Down

0 comments on commit f2c2253

Please sign in to comment.