Skip to content

Commit 86de2ed

Browse files
[Process] Make tests more deterministic
1 parent 6a92f4e commit 86de2ed

File tree

3 files changed

+174
-201
lines changed

3 files changed

+174
-201
lines changed

src/Symfony/Component/Process/Process.php

+34-56
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ class Process
7676

7777
private static $sigchild;
7878
private static $posixSignals = array(
79-
1 => 1, // SIGHUP
80-
2 => 2, // SIGINT
81-
3 => 3, // SIGQUIT
82-
6 => 6, // SIGABRT
83-
14 => 14, // SIGALRM
84-
15 => 15, // SIGTERM
79+
1, // SIGHUP
80+
2, // SIGINT
81+
3, // SIGQUIT
82+
6, // SIGABRT
83+
14, // SIGALRM
84+
15, // SIGTERM
8585
);
8686

8787
/**
@@ -285,27 +285,33 @@ public function start($callback = null)
285285
if (!isset($this->options['bypass_shell'])) {
286286
$this->options['bypass_shell'] = true;
287287
}
288-
}
288+
} elseif (!$this->useFileHandles && $this->enhanceSigchildCompatibility && $this->isSigchildEnabled()) {
289+
// last exit code is output on the fourth pipe and caught to work around --enable-sigchild
290+
$descriptors[3] = array('pipe', 'w');
291+
292+
$commandline = '';
293+
foreach (self::$posixSignals as $s) {
294+
$commandline .= "trap 'echo s$s >&3' $s;";
295+
}
289296

290-
$ptsWorkaround = null;
297+
$commandline .= '{ ('.$this->commandline.') <&3 3<&- 3>/dev/null & } 3<&0;';
298+
$commandline .= 'pid=$!; echo $pid >&3; wait $pid; code=$?; echo x$code >&3; exit $code';
291299

292-
if (!$this->useFileHandles && $this->enhanceSigchildCompatibility && $this->isSigchildEnabled()) {
293300
// Workaround for the bug, when PTS functionality is enabled.
294301
// @see : https://bugs.php.net/69442
295302
$ptsWorkaround = fopen(__FILE__, 'r');
296303
}
297304

298305
$this->process = proc_open($commandline, $descriptors, $this->processPipes->pipes, $this->cwd, $this->env, $this->options);
299306

300-
if ($ptsWorkaround) {
301-
fclose($ptsWorkaround);
302-
}
303-
304307
if (!is_resource($this->process)) {
305308
throw new RuntimeException('Unable to launch a new process.');
306309
}
307310
$this->status = self::STATUS_STARTED;
308311

312+
if (isset($descriptors[3])) {
313+
$this->fallbackStatus['pid'] = (int) fgets($this->processPipes->pipes[3]);
314+
}
309315
if ($this->tty) {
310316
return;
311317
}
@@ -596,8 +602,6 @@ public function clearErrorOutput()
596602
public function getExitCode()
597603
{
598604
if (!$this->enhanceSigchildCompatibility && $this->isSigchildEnabled()) {
599-
$this->stop(0);
600-
601605
throw new RuntimeException('This PHP has been compiled with --enable-sigchild. You must use setEnhanceSigchildCompatibility() to use this method.');
602606
}
603607

@@ -651,8 +655,6 @@ public function hasBeenSignaled()
651655
$this->requireProcessIsTerminated(__FUNCTION__);
652656

653657
if (!$this->enhanceSigchildCompatibility && $this->isSigchildEnabled()) {
654-
$this->stop(0);
655-
656658
throw new RuntimeException('This PHP has been compiled with --enable-sigchild. Term signal can not be retrieved.');
657659
}
658660

@@ -674,8 +676,6 @@ public function getTermSignal()
674676
$this->requireProcessIsTerminated(__FUNCTION__);
675677

676678
if ($this->isSigchildEnabled() && (!$this->enhanceSigchildCompatibility || -1 === $this->processInformation['termsig'])) {
677-
$this->stop(0);
678-
679679
throw new RuntimeException('This PHP has been compiled with --enable-sigchild. Term signal can not be retrieved.');
680680
}
681681

@@ -1223,14 +1223,7 @@ public static function isPtySupported()
12231223
return $result = false;
12241224
}
12251225

1226-
$proc = @proc_open('echo 1', array(array('pty'), array('pty'), array('pty')), $pipes);
1227-
if (is_resource($proc)) {
1228-
proc_close($proc);
1229-
1230-
return $result = true;
1231-
}
1232-
1233-
return $result = false;
1226+
return $result = (bool) @proc_open('echo 1', array(array('pty'), array('pty'), array('pty')), $pipes);
12341227
}
12351228

12361229
/**
@@ -1245,22 +1238,8 @@ private function getDescriptors()
12451238
} else {
12461239
$this->processPipes = UnixPipes::create($this, $this->input);
12471240
}
1248-
$descriptors = $this->processPipes->getDescriptors($this->outputDisabled);
1249-
1250-
if (!$this->useFileHandles && $this->enhanceSigchildCompatibility && $this->isSigchildEnabled()) {
1251-
// last exit code is output on the fourth pipe and caught to work around --enable-sigchild
1252-
$descriptors[3] = array('pipe', 'w');
12531241

1254-
$trap = '';
1255-
foreach (self::$posixSignals as $s) {
1256-
$trap .= "trap 'echo s$s >&3' $s;";
1257-
}
1258-
1259-
$this->commandline = $trap.'{ ('.$this->commandline.') <&3 3<&- 3>/dev/null & } 3<&0;';
1260-
$this->commandline .= 'pid=$!; echo p$pid >&3; wait $pid; code=$?; echo x$code >&3; exit $code';
1261-
}
1262-
1263-
return $descriptors;
1242+
return $this->processPipes->getDescriptors($this->outputDisabled);
12641243
}
12651244

12661245
/**
@@ -1462,14 +1441,6 @@ private function doSignal($signal, $throwException)
14621441
return false;
14631442
}
14641443

1465-
if ($this->enhanceSigchildCompatibility && $this->isSigchildEnabled() && !isset(self::$posixSignals[$signal]) && !(function_exists('posix_kill') && @posix_kill($this->getPid(), $signal))) {
1466-
if ($throwException) {
1467-
throw new RuntimeException('This PHP has been compiled with --enable-sigchild and posix_kill() is not available.');
1468-
}
1469-
1470-
return false;
1471-
}
1472-
14731444
if ('\\' === DIRECTORY_SEPARATOR) {
14741445
exec(sprintf('taskkill /F /T /PID %d 2>&1', $this->getPid()), $output, $exitCode);
14751446
if ($exitCode && $this->isRunning()) {
@@ -1479,14 +1450,21 @@ private function doSignal($signal, $throwException)
14791450

14801451
return false;
14811452
}
1482-
}
1483-
1484-
if (true !== @proc_terminate($this->process, $signal) && '\\' !== DIRECTORY_SEPARATOR) {
1485-
if ($throwException) {
1486-
throw new RuntimeException(sprintf('Error while sending signal `%s`.', $signal));
1453+
} else {
1454+
if (!$this->enhanceSigchildCompatibility || !$this->isSigchildEnabled()) {
1455+
$ok = @proc_terminate($this->process, $signal);
1456+
} elseif (function_exists('posix_kill')) {
1457+
$ok = @posix_kill($this->getPid(), $signal);
1458+
} elseif ($ok = proc_open(sprintf('kill -%d %d', $signal, $this->getPid()), array(2 => array('pipe', 'w')), $pipes)) {
1459+
$ok = false === fgets($pipes[2]);
14871460
}
1461+
if (!$ok) {
1462+
if ($throwException) {
1463+
throw new RuntimeException(sprintf('Error while sending signal `%s`.', $signal));
1464+
}
14881465

1489-
return false;
1466+
return false;
1467+
}
14901468
}
14911469

14921470
$this->latestSignal = (int) $signal;

src/Symfony/Component/Process/Tests/NonStopableProcess.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,18 @@ function handleSignal($signal)
3030
break;
3131
}
3232

33-
echo "received signal $name\n";
33+
echo "signal $name\n";
3434
}
3535

36-
declare (ticks = 1);
3736
pcntl_signal(SIGTERM, 'handleSignal');
3837
pcntl_signal(SIGINT, 'handleSignal');
3938

39+
echo 'received ';
40+
4041
$duration = isset($argv[1]) ? (int) $argv[1] : 3;
4142
$start = microtime(true);
4243

4344
while ($duration > (microtime(true) - $start)) {
44-
usleep(1000);
45+
usleep(10000);
46+
pcntl_signal_dispatch();
4547
}

0 commit comments

Comments
 (0)