Skip to content

Commit 77b1a9f

Browse files
committed
Do not remove PID file if not our own process
1 parent df654ba commit 77b1a9f

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

src/Commands/StartCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
4646
$handler->setTtl($config['ttl']);
4747
$handler->setPhpCgiExecutable($config['cgi-path']);
4848
$handler->setSocketPath($config['socket-path']);
49-
$handler->setPIDFile($config['pidfile']);
49+
$handler->setPidFile($config['pidfile']);
5050
$handler->setPopulateServer($config['populate-server-var']);
5151
$handler->setStaticDirectory($config['static-directory']);
5252
$handler->run();

src/ProcessManager.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ class ProcessManager
229229
/**
230230
* Location of the file where we're going to store the PID of the master process
231231
*/
232-
protected $pidfile;
232+
protected $pidFile;
233233

234234
/**
235235
* Controller port
@@ -324,7 +324,7 @@ private function quit()
324324
$this->loop->stop();
325325
}
326326

327-
unlink($this->pidfile);
327+
$this->removePidFile();
328328
exit;
329329
}
330330

@@ -480,9 +480,9 @@ public function setStaticDirectory($staticDirectory)
480480
$this->staticDirectory = $staticDirectory;
481481
}
482482

483-
public function setPIDFile($pidfile)
483+
public function setPidFile($pidFile)
484484
{
485-
$this->pidfile = $pidfile;
485+
$this->pidFile = $pidFile;
486486
}
487487
/**
488488
* @return boolean
@@ -530,12 +530,13 @@ public function run()
530530
ob_implicit_flush(1);
531531

532532
$this->loop = Factory::create();
533-
$this->controller = new UnixServer($this->getControllerSocketPath(), $this->loop);
534-
$this->controller->on('connection', [$this, 'onSlaveConnection']);
535533

536534
$this->web = new Server(sprintf('%s:%d', $this->host, $this->port), $this->loop, ['backlog' => self::TCP_BACKLOG]);
537535
$this->web->on('connection', [$this, 'onRequest']);
538536

537+
$this->controller = new UnixServer($this->getControllerSocketPath(), $this->loop);
538+
$this->controller->on('connection', [$this, 'onSlaveConnection']);
539+
539540
$this->loop->addSignal(SIGTERM, [$this, 'shutdown']);
540541
$this->loop->addSignal(SIGINT, [$this, 'shutdown']);
541542
$this->loop->addSignal(SIGCHLD, [$this, 'handleSigchld']);
@@ -551,7 +552,7 @@ public function run()
551552
$loopClass = (new \ReflectionClass($this->loop))->getShortName();
552553

553554
$this->output->writeln("<info>Starting PHP-PM with {$this->slaveCount} workers, using {$loopClass} ...</info>");
554-
$this->writePid();
555+
$this->writePidFile();
555556

556557
$this->createSlaves();
557558

@@ -566,10 +567,20 @@ public function handleSigchld()
566567
$pid = pcntl_waitpid(-1, $status, WNOHANG);
567568
}
568569

569-
public function writePid()
570+
private function writePidFile()
570571
{
571572
$pid = getmypid();
572-
file_put_contents($this->pidfile, $pid);
573+
file_put_contents($this->pidFile, $pid);
574+
}
575+
576+
private function removePidFile()
577+
{
578+
$pid = getmypid();
579+
$actualPid = (int) file_get_contents($this->pidFile);
580+
//Only remove the pid file if it is our own
581+
if ($actualPid === $pid) {
582+
unlink($this->pidFile);
583+
}
573584
}
574585

575586
/**

0 commit comments

Comments
 (0)