Skip to content

Commit

Permalink
Also fix zombies where process is killed
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Jul 6, 2022
1 parent 8c769ff commit 76e9495
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
22 changes: 22 additions & 0 deletions lib/Internal/Posix/Handle.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Amp\Process\Internal\Posix;

use Amp\Deferred;
use Amp\Loop;
use Amp\Process\Internal\ProcessHandle;

/** @internal */
Expand Down Expand Up @@ -35,4 +36,25 @@ public function __construct()

/** @var int */
public $shellPid;

public function wait()
{
if ($this->shellPid === 0) {
return;
}

$pid = $this->shellPid;
$this->shellPid = 0;

Loop::unreference(Loop::repeat(100, static function (string $watcherId) use ($pid) {
if (!\extension_loaded('pcntl') || \pcntl_waitpid($pid, $status, \WNOHANG) !== 0) {
Loop::cancel($watcherId);
}
}));
}

public function __destruct()
{
$this->wait();
}
}
4 changes: 1 addition & 3 deletions lib/Internal/Posix/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ public static function onProcessEndExtraDataPipeReadable($watcher, $stream, Hand
$handle->joinDeferred->resolve((int) \rtrim(@\stream_get_contents($stream)));
}

if (\extension_loaded('pcntl')) {
\pcntl_waitpid($handle->shellPid, $status, \WNOHANG);
}
$handle->wait();
}

public static function onProcessStartExtraDataPipeReadable($watcher, $stream, $data)
Expand Down

0 comments on commit 76e9495

Please sign in to comment.