Skip to content
This repository was archived by the owner on Nov 30, 2023. It is now read-only.

Commit 1f9b6c4

Browse files
committed
Use zombie-reaper
1 parent bcff1cb commit 1f9b6c4

File tree

7 files changed

+127
-89
lines changed

7 files changed

+127
-89
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"react/promise-stream": "^1.3",
2323
"react/child-process": "^0.6",
2424
"react/promise": "^2.8|^2.9",
25-
"react/event-loop": "^1.2|^1.3"
25+
"react/event-loop": "^1.2|^1.3",
26+
"choval/zombie-reaper": "^1.0"
2627
},
2728
"suggest": {
2829
"ext-pcntl": "Required to create childs to async blocking code",

composer.lock

Lines changed: 32 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Async.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ protected static function waitProcessExits(int $pid, ?LoopInterface $loop = null
305305
}
306306
$defer = new Deferred();
307307
$timer = $loop->addPeriodicTimer(0.001, function () use ($pid, $defer) {
308-
$r = pcntl_waitpid($pid, $status, \WNOHANG);
309-
if (!$r) {
308+
$r = zombie_reap($pid);
309+
if ($r >= 0) {
310310
$defer->resolve($r);
311311
}
312312
});
@@ -333,10 +333,8 @@ protected static function killProcess(int $pid, int $signal=15, ?LoopInterface $
333333
foreach ($pids as $pid) {
334334
$pid = intval($pid);
335335
if ($pid) {
336-
posix_kill($pid, $signal);
337-
if ($loop) {
338-
$promises[] = static::waitProcessExits($pid, $loop);
339-
}
336+
$promises[$pid] = static::waitProcessExits($pid, $loop);
337+
zombie_kill($pid);
340338
}
341339
}
342340
if ($loop) {
@@ -432,6 +430,10 @@ public static function executeWithLoop(LoopInterface $loop, string $cmd, float $
432430
foreach ($proc->pipes as $pipe) {
433431
$pipe->close();
434432
}
433+
$pid = $proc->getPid();
434+
if ($pid) {
435+
static::killProcess($pid, 15, $loop);
436+
}
435437
if ($err) {
436438
return $defer->reject($err);
437439
}
@@ -442,10 +444,6 @@ public static function executeWithLoop(LoopInterface $loop, string $cmd, float $
442444
return $defer->reject(new Exception('Process exited with code: ' . $exitCode . "\n$buffer", $exitCode, $trace));
443445
}
444446
$defer->resolve($buffer);
445-
$pid = $proc->getPid();
446-
if ($pid) {
447-
static::killProcess($pid, 15, $loop);
448-
}
449447
});
450448
});
451449
return $defer->promise();
@@ -599,7 +597,7 @@ public static function asyncWithLoop(LoopInterface $loop, $fn, array $args = [])
599597
$pid = false;
600598
$id = bin2hex(random_bytes(16));
601599
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
602-
$defer = new Deferred(function () use (&$pid, $loop) {
600+
$defer = new Deferred(function () use ($id, &$pid, $loop) {
603601
static::removeFork($id);
604602
if ($pid) {
605603
static::killProcess($pid, 9, $loop);

0 commit comments

Comments
 (0)