Skip to content

Commit

Permalink
#7: Do not execute parent's shutdown function in child process
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLevti committed Feb 1, 2020
1 parent 3d58dda commit 925fc36
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
14 changes: 10 additions & 4 deletions src/Spork/ProcessManager.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php

/*
* This file is part of Spork, an OpenSky project.
* This file is part of the thelevti/spork package.
*
* (c) OpenSky Project Inc
* (c) Petr Levtonov <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Spork;

use InvalidArgumentException;
Expand Down Expand Up @@ -95,6 +97,7 @@ public function fork($callable)
}

if (0 === $pid) {
$currPid = posix_getpid();
// reset the list of child processes
$this->forks = [];

Expand All @@ -103,8 +106,11 @@ public function fork($callable)
$message = new ExitMessage();

// phone home on shutdown
register_shutdown_function(function () use ($shm, $message): void {
$status = null;
register_shutdown_function(function () use ($currPid, $shm, $message): void {
// Do not execute this function in child processes.
if ($currPid !== posix_getpid()) {
return;
}

try {
$shm->send($message, false);
Expand Down
6 changes: 4 additions & 2 deletions tests/Spork/ProcessManagerTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php

/*
* This file is part of Spork, an OpenSky project.
* This file is part of the thelevti/spork package.
*
* (c) OpenSky Project Inc
* (c) Petr Levtonov <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Spork;

use Exception;
Expand Down

0 comments on commit 925fc36

Please sign in to comment.