Skip to content

Commit

Permalink
Fix exit code on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Jun 15, 2017
1 parent fb0a645 commit 90a18ad
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Amp\ByteStream\ResourceInputStream;
use Amp\ByteStream\ResourceOutputStream;
use Amp\Deferred;
use Amp\Delayed;
use Amp\Loop;
use Amp\Promise;

Expand Down Expand Up @@ -177,9 +178,11 @@ public function start() {

if (self::$onWindows) {
$this->pid = $status["pid"];
$exitcode = $status["exitcode"];
} else {
// This blocking read will only block until the process scheduled, generally a few microseconds.
$pid = \rtrim(@\fgets($pipes[3]));
$exitcode = -1;

if (!$pid || !\is_numeric($pid)) {
$deferred->fail(new ProcessException("Could not determine PID"));
Expand All @@ -199,18 +202,23 @@ public function start() {
$process = &$this->process;
$running = &$this->running;
$this->watcher = Loop::onReadable($pipes[3], static function ($watcher, $resource) use (
&$process, &$running, $deferred
&$process, &$running, $exitcode, $deferred
) {
Loop::cancel($watcher);
$running = false;

try {
try {
if (!\is_resource($resource) || \feof($resource)) {
throw new ProcessException("Process ended unexpectedly");
}
if (self::$onWindows) {
$code = \proc_get_status($process)["exitcode"];
$status = \proc_get_status($process);

while ($status["running"]) {
$status = \proc_get_status($process);
}

$code = $exitcode !== -1 ? $exitcode : $status["exitcode"];
} else if (!\is_resource($resource) || \feof($resource)) {
throw new ProcessException("Process ended unexpectedly");
} else {
$code = \rtrim(@\stream_get_contents($resource));
}
Expand Down

0 comments on commit 90a18ad

Please sign in to comment.