From 5f0535934d72f0c05ae56c653dd8cc7dd29e3ba4 Mon Sep 17 00:00:00 2001 From: epriestley Date: Thu, 23 Jul 2020 09:27:14 -0700 Subject: [PATCH] Manage PIDs more carefully in DaemonHandle Summary: Ref T13555. Although these callsites may not actually impact anything, it's possible for an active handle to have no PID (e.g., if the subprocess failed to start). Handle these cases more carefully. Test Plan: Started daemons, saw them run fine. See also next change. Maniphest Tasks: T13555 Differential Revision: https://secure.phabricator.com/D21424 --- .../daemon/PhutilDaemonHandle.php | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/infrastructure/daemon/PhutilDaemonHandle.php b/src/infrastructure/daemon/PhutilDaemonHandle.php index 5030f5330c..25c517b8cf 100644 --- a/src/infrastructure/daemon/PhutilDaemonHandle.php +++ b/src/infrastructure/daemon/PhutilDaemonHandle.php @@ -327,13 +327,14 @@ final class PhutilDaemonHandle extends Phobject { private function annihilateProcessGroup() { $pid = $this->getPID(); - - $pgid = posix_getpgid($pid); - if ($pid && $pgid) { - posix_kill(-$pgid, SIGTERM); - sleep($this->getKillDelay()); - posix_kill(-$pgid, SIGKILL); - $this->pid = null; + if ($pid) { + $pgid = posix_getpgid($pid); + if ($pgid) { + posix_kill(-$pgid, SIGTERM); + sleep($this->getKillDelay()); + posix_kill(-$pgid, SIGKILL); + $this->pid = null; + } } } @@ -440,7 +441,10 @@ final class PhutilDaemonHandle extends Phobject { // naturally be restarted after it exits, as though it had exited after an // unhandled exception. - posix_kill($this->getPID(), SIGINT); + $pid = $this->getPID(); + if ($pid) { + posix_kill($pid, SIGINT); + } } public function didReceiveGracefulSignal($signo) { @@ -461,7 +465,10 @@ final class PhutilDaemonHandle extends Phobject { $this->logMessage('DONE', $sigmsg, $signo); - posix_kill($this->getPID(), SIGINT); + $pid = $this->getPID(); + if ($pid) { + posix_kill($pid, SIGINT); + } } public function didReceiveTerminateSignal($signo) {