Skip to content

Commit 5dc2bb3

Browse files
Merge branch '2.3' into 2.7
* 2.3: [PropertyAccess] Reorder elements array after PropertyPathBuilder::replace [Routing] Skip PhpGeneratorDumperTest::testDumpWithTooManyRoutes on HHVM [Process] More robustness and deterministic tests Conflicts: src/Symfony/Component/Process/Process.php src/Symfony/Component/Process/Tests/ProcessTest.php
2 parents 68b3d2d + b598fca commit 5dc2bb3

File tree

7 files changed

+165
-175
lines changed

7 files changed

+165
-175
lines changed

src/Symfony/Component/Process/PhpProcess.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ public function __construct($script, $cwd = null, array $env = null, $timeout =
4646
$php .= ' '.ProcessUtils::escapeArgument($file);
4747
$script = null;
4848
}
49+
if ('\\' !== DIRECTORY_SEPARATOR && null !== $php) {
50+
// exec is mandatory to deal with sending a signal to the process
51+
// see https://github.com/symfony/symfony/issues/5030 about prepending
52+
// command with exec
53+
$php = 'exec '.$php;
54+
}
4955

5056
parent::__construct($php, $cwd, $env, $script, $timeout, $options);
5157
}

src/Symfony/Component/Process/Process.php

Lines changed: 40 additions & 50 deletions
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,35 @@ 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+
// See https://unix.stackexchange.com/questions/71205/background-process-pipe-input
298+
$commandline .= '{ ('.$this->commandline.') <&3 3<&- 3>/dev/null & } 3<&0;';
299+
$commandline .= 'pid=$!; echo $pid >&3; wait $pid; code=$?; echo x$code >&3; exit $code';
291300

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

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

300-
if ($ptsWorkaround) {
301-
fclose($ptsWorkaround);
302-
}
303-
304308
if (!is_resource($this->process)) {
305309
throw new RuntimeException('Unable to launch a new process.');
306310
}
307311
$this->status = self::STATUS_STARTED;
308312

313+
if (isset($descriptors[3])) {
314+
$this->fallbackStatus['pid'] = (int) fgets($this->processPipes->pipes[3]);
315+
}
316+
309317
if ($this->tty) {
310318
return;
311319
}
@@ -596,8 +604,6 @@ public function clearErrorOutput()
596604
public function getExitCode()
597605
{
598606
if (!$this->enhanceSigchildCompatibility && $this->isSigchildEnabled()) {
599-
$this->stop(0);
600-
601607
throw new RuntimeException('This PHP has been compiled with --enable-sigchild. You must use setEnhanceSigchildCompatibility() to use this method.');
602608
}
603609

@@ -651,8 +657,6 @@ public function hasBeenSignaled()
651657
$this->requireProcessIsTerminated(__FUNCTION__);
652658

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

@@ -674,8 +678,6 @@ public function getTermSignal()
674678
$this->requireProcessIsTerminated(__FUNCTION__);
675679

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

@@ -1245,22 +1247,8 @@ private function getDescriptors()
12451247
} else {
12461248
$this->processPipes = UnixPipes::create($this, $this->input);
12471249
}
1248-
$descriptors = $this->processPipes->getDescriptors($this->outputDisabled);
12491250

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');
1253-
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;
1251+
return $this->processPipes->getDescriptors($this->outputDisabled);
12641252
}
12651253

12661254
/**
@@ -1379,8 +1367,11 @@ private function readPipes($blocking, $close)
13791367
$this->fallbackStatus['signaled'] = true;
13801368
$this->fallbackStatus['exitcode'] = -1;
13811369
$this->fallbackStatus['termsig'] = (int) substr($data, 1);
1382-
} elseif ('x' === $data[0] && !isset($this->fallbackStatus['signaled'])) {
1383-
$this->fallbackStatus['exitcode'] = (int) substr($data, 1);
1370+
} elseif ('x' === $data[0]) {
1371+
$this->fallbackStatus['running'] = false;
1372+
if (!isset($this->fallbackStatus['signaled'])) {
1373+
$this->fallbackStatus['exitcode'] = (int) substr($data, 1);
1374+
}
13841375
}
13851376
}
13861377
} else {
@@ -1462,14 +1453,6 @@ private function doSignal($signal, $throwException)
14621453
return false;
14631454
}
14641455

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-
14731456
if ('\\' === DIRECTORY_SEPARATOR) {
14741457
exec(sprintf('taskkill /F /T /PID %d 2>&1', $this->getPid()), $output, $exitCode);
14751458
if ($exitCode && $this->isRunning()) {
@@ -1479,14 +1462,21 @@ private function doSignal($signal, $throwException)
14791462

14801463
return false;
14811464
}
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));
1465+
} else {
1466+
if (!$this->enhanceSigchildCompatibility || !$this->isSigchildEnabled()) {
1467+
$ok = @proc_terminate($this->process, $signal);
1468+
} elseif (function_exists('posix_kill')) {
1469+
$ok = @posix_kill($this->getPid(), $signal);
1470+
} elseif ($ok = proc_open(sprintf('kill -%d %d', $signal, $this->getPid()), array(2 => array('pipe', 'w')), $pipes)) {
1471+
$ok = false === fgets($pipes[2]);
14871472
}
1473+
if (!$ok) {
1474+
if ($throwException) {
1475+
throw new RuntimeException(sprintf('Error while sending signal `%s`.', $signal));
1476+
}
14881477

1489-
return false;
1478+
return false;
1479+
}
14901480
}
14911481

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

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

Lines changed: 5 additions & 3 deletions
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)