mirror of
https://code.mensbeam.com/MensBeam/Arsse.git
synced 2024-12-22 13:12:41 +00:00
Handle exceptions from child processes
This commit is contained in:
parent
e9394e8599
commit
e160189224
2 changed files with 48 additions and 34 deletions
|
@ -6,6 +6,8 @@
|
|||
declare(strict_types=1);
|
||||
namespace JKingWeb\Arsse\Service;
|
||||
|
||||
use JKingWeb\Arsse\AbstractException;
|
||||
|
||||
class Daemon {
|
||||
protected const PID_PATTERN = '/^([1-9]\d{0,77})?$/D'; // no more than 78 digits (256-bit unsigned integer), starting with a digit other than zero
|
||||
|
||||
|
@ -34,7 +36,10 @@ class Daemon {
|
|||
case 0:
|
||||
fclose($pipe[0]);
|
||||
# In the child, call setsid() to detach from any terminal and create an independent session.
|
||||
@posix_setsid();
|
||||
try {
|
||||
if (@posix_setsid() === -1) {
|
||||
throw new Exception("forkFailed", ['instance' => 1]);
|
||||
}
|
||||
# In the child, call fork() again, to ensure that the daemon can never re-acquire a terminal again. (This relevant if the program — and all its dependencies — does not carefully specify `O_NOCTTY` on each and every single `open()` call that might potentially open a TTY device node.)
|
||||
switch (@pcntl_fork()) {
|
||||
case -1:
|
||||
|
@ -49,7 +54,7 @@ class Daemon {
|
|||
# From the daemon process, notify the original process started that initialization is complete. This can be implemented via an unnamed pipe or similar communication channel that is created before the first fork() and hence available in both the original and the daemon process.
|
||||
fwrite($pipe[1], (string) posix_getpid());
|
||||
fclose($pipe[1]);
|
||||
// now everything else is done in order
|
||||
// now everything else is done in order, but beyond this point any errors cannot be reported back to the original process
|
||||
# In the daemon process, connect /dev/null to standard input, output, and error.
|
||||
fclose(STDIN);
|
||||
fclose(STDOUT);
|
||||
|
@ -67,9 +72,18 @@ class Daemon {
|
|||
# Call exit() in the first child, so that only the second child (the actual daemon process) stays around. This ensures that the daemon process is re-parented to init/PID 1, as all daemons should be.
|
||||
exit;
|
||||
}
|
||||
} catch (AbstractException $e) {
|
||||
// transmit the exception back to the original process, which will re-create the exception if necessary
|
||||
@fwrite($pipe[1], json_encode([get_class($e), $e->getSymbol(), $e->getParams()]));
|
||||
exit;
|
||||
}
|
||||
default:
|
||||
fclose($pipe[1]);
|
||||
$result = fread($pipe[0], 100);
|
||||
$result = json_decode(fread($pipe[0], 100), true);
|
||||
if ($result) {
|
||||
[$class, $symbol, $params] = $result;
|
||||
throw new $class($symbol, $params);
|
||||
}
|
||||
fclose($pipe[0]);
|
||||
# Call exit() in the original process. The process that invoked the daemon must be able to rely on that this exit() happens after initialization is complete and all external communication channels are established and accessible.
|
||||
exit;
|
||||
|
|
|
@ -96,7 +96,7 @@ class TestCLI extends \JKingWeb\Arsse\Test\AbstractTest {
|
|||
$srv = $this->mock(Service::class);
|
||||
$srv->watch->returns(new \DateTimeImmutable);
|
||||
$daemon = $this->mock(Daemon::class);
|
||||
$daemon->checkPIDFilePath->throws(new Service\Exception("pidDuplicate", ['pid' =>2112]));
|
||||
$daemon->checkPIDFilePath->throws(new Service\Exception("pidDuplicate", ['pid' => 2112]));
|
||||
$daemon->fork->returns(null);
|
||||
$this->objMock->get->with(Service::class)->returns($srv->get());
|
||||
$this->objMock->get->with(Daemon::class)->returns($daemon->get());
|
||||
|
|
Loading…
Reference in a new issue