Skip to content

fix signal handling in wait() on calls to stop() #11436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Symfony/Component/Process/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class Process
/** @var ProcessPipes */
private $processPipes;

private $latestSignal;

private static $sigchild;

/**
Expand Down Expand Up @@ -321,7 +323,7 @@ public function wait($callback = null)
usleep(1000);
}

if ($this->processInformation['signaled']) {
if ($this->processInformation['signaled'] && $this->processInformation['termsig'] !== $this->latestSignal) {
throw new RuntimeException(sprintf('The process has been signaled with signal "%s".', $this->processInformation['termsig']));
}

Expand Down Expand Up @@ -661,7 +663,8 @@ public function stop($timeout = 10, $signal = null)
throw new RuntimeException('Unable to kill the process');
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment regarding the line below?

// given `SIGTERM` may not be defined and that `proc_terminate` uses the constant value and not the constant itself, we use the same here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, it's done now. Do you think we should replace the usage of those constants in some of the other lines where they are used?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, not needed

proc_terminate($this->process);
// given `SIGTERM` may not be defined and that `proc_terminate` uses the constant value and not the constant itself, we use the same here
$this->doSignal(15, false);
do {
usleep(1000);
} while ($this->isRunning() && microtime(true) < $timeoutMicro);
Expand Down Expand Up @@ -1158,6 +1161,7 @@ private function resetProcessData()
$this->stdout = null;
$this->stderr = null;
$this->process = null;
$this->latestSignal = null;
$this->status = self::STATUS_READY;
$this->incrementalOutputOffset = 0;
$this->incrementalErrorOutputOffset = 0;
Expand Down Expand Up @@ -1201,6 +1205,8 @@ private function doSignal($signal, $throwException)
return false;
}

$this->latestSignal = $signal;

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ public function testExitCodeIsAvailableAfterSignal()
$this->markTestSkipped('Signal is not supported in sigchild environment');
}

public function testRunProcessWithTimeout()
{
$this->markTestSkipped('Signal (required for timeout) is not supported in sigchild environment');
}

/**
* {@inheritdoc}
*/
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Process/Tests/SigchildEnabledProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ public function testStartAfterATimeout()
parent::testStartAfterATimeout();
}

public function testStopWithTimeoutIsActuallyWorking()
{
$this->markTestSkipped('Stopping with signal is not supported in sigchild environment');
}

public function testRunProcessWithTimeout()
{
$this->markTestSkipped('Signal (required for timeout) is not supported in sigchild environment');
}

public function testCheckTimeoutOnStartedProcess()
{
$this->markTestSkipped('Signal (required for timeout) is not supported in sigchild environment');
}

/**
* {@inheritdoc}
*/
Expand Down
51 changes: 51 additions & 0 deletions src/Symfony/Component/Process/Tests/SimpleProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,57 @@ public function testSignalWithWrongNonIntSignal()
parent::testSignalWithWrongNonIntSignal();
}

public function testStopTerminatesProcessCleanly()
{
try {
$process = $this->getProcess('php -r "echo \'foo\'; sleep(1); echo \'bar\';"');
$process->run(function () use ($process) {
$process->stop();
});
} catch (RuntimeException $e) {
$this->fail('A call to stop() is not expected to cause wait() to throw a RuntimeException');
}
}

public function testKillSignalTerminatesProcessCleanly()
{
$this->expectExceptionIfPHPSigchild('Symfony\Component\Process\Exception\RuntimeException', 'This PHP has been compiled with --enable-sigchild. The process can not be signaled.');

try {
$process = $this->getProcess('php -r "echo \'foo\'; sleep(1); echo \'bar\';"');
$process->run(function () use ($process) {
if ($process->isRunning()) {
$process->signal(SIGKILL);
}
});
} catch (RuntimeException $e) {
$this->fail('A call to signal() is not expected to cause wait() to throw a RuntimeException');
}
}

public function testTermSignalTerminatesProcessCleanly()
{
$this->expectExceptionIfPHPSigchild('Symfony\Component\Process\Exception\RuntimeException', 'This PHP has been compiled with --enable-sigchild. The process can not be signaled.');

try {
$process = $this->getProcess('php -r "echo \'foo\'; sleep(1); echo \'bar\';"');
$process->run(function () use ($process) {
if ($process->isRunning()) {
$process->signal(SIGTERM);
}
});
} catch (RuntimeException $e) {
$this->fail('A call to signal() is not expected to cause wait() to throw a RuntimeException');
}
}

public function testStopWithTimeoutIsActuallyWorking()
{
$this->skipIfPHPSigchild();

parent::testStopWithTimeoutIsActuallyWorking();
}

/**
* {@inheritdoc}
*/
Expand Down