8000 Merge branch '2.7' into 2.8 · SCIF/symfony@7fe0a3b · GitHub
[go: up one dir, main page]

Skip to content

Commit 7fe0a3b

Browse files
Merge branch '2.7' into 2.8
* 2.7: [Process] Fix transient test on Windows [Process] Make tests more deterministic [PropertyAccess] Reorder elements array after PropertyPathBuilder::replace [Routing] Skip PhpGeneratorDumperTest::testDumpWithTooManyRoutes on HHVM [Process] More robustness and deterministic tests
2 parents 0527344 + 3bb3f3b commit 7fe0a3b

File tree

7 files changed

+202
-213
lines changed

7 files changed

+202
-213
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: 41 additions & 58 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

@@ -1223,14 +1225,7 @@ public static function isPtySupported()
12231225
return $result = false;
12241226
}
12251227

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;
1228+
return $result = (bool) @proc_open('echo 1', array(array('pty'), array('pty'), array('pty')), $pipes);
12341229
}
12351230

12361231
/**
@@ -1245,22 +1240,8 @@ private function getDescriptors()
12451240
} else {
12461241
$this->processPipes = UnixPipes::create($this, $this->input);
12471242
}
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');
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-
}
12621243

1263-
return $descriptors;
1244+
return $this->processPipes->getDescriptors($this->outputDisabled);
12641245
}
12651246

12661247
/**
@@ -1379,8 +1360,11 @@ private function readPipes($blocking, $close)
13791360
$this->fallbackStatus['signaled'] = true;
13801361
$this->fallbackStatus['exitcode'] = -1;
13811362
$this->fallbackStatus['termsig'] = (int) substr($data, 1);
1382-
} elseif ('x' === $data[0] && !isset($this->fallbackStatus['signaled'])) {
1383-
$this->fallbackStatus['exitcode'] = (int) substr($data, 1);
1363+
} elseif ('x' === $data[0]) {
1364+
$this->fallbackStatus['running'] = false;
1365+
if (!isset($this->fallbackStatus['signaled'])) {
1366+
$this->fallbackStatus['exitcode'] = (int) substr($data, 1);
1367+
}
13841368
}
13851369
}
13861370
} else {
@@ -1462,14 +1446,6 @@ private function doSignal($signal, $throwException)
14621446
return false;
14631447
}
14641448

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

14801456
return false;
14811457
}
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));
1458+
} else {
1459+
if (!$this->enhanceSigchildCompatibility || !$this->isSigchildEnabled()) {
1460+
$ok = @proc_terminate($this->process, $signal);
1461+
} elseif (function_exists('posix_kill')) {
1462+
$ok = @posix_kill($this->getPid(), $signal);
1463+
} elseif ($ok = proc_open(sprintf('kill -%d %d', $signal, $this->getPid()), array(2 => array('pipe', 'w')), $pipes)) {
1464+
$ok = false === fgets($pipes[2]);
14871465
}
1466+
if (!$ok) {
1467+
if ($throwException) {
1468+
throw new RuntimeException(sprintf('Error while sending signal `%s`.', $signal));
1469+
}
14881470

1489-
return false;
1471+
return false;
1472+
}
14901473
}
14911474

14921475
$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)
0