Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
[Process] Fix Inconsistent Exit Status in proc_get_status for PHP Ver…
…sions Below 8.3
  • Loading branch information
Luc45 authored and nicolas-grekas committed Feb 9, 2024
commit 500caa3f099596fdc4ca824b87e91f86bea299dd
14 changes: 14 additions & 0 deletions src/Symfony/Component/Process/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class Process implements \IteratorAggregate
private $processPipes;

private $latestSignal;
private $cachedExitCode;

private static $sigchild;

Expand Down Expand Up @@ -1345,6 +1346,19 @@ protected function updateStatus(bool $blocking)
$this->processInformation = proc_get_status($this->process);
$running = $this->processInformation['running'];

// In PHP < 8.3, "proc_get_status" only returns the correct exit status on the first call.
// Subsequent calls return -1 as the process is discarded. This workaround caches the first
// retrieved exit status for consistent results in later calls, mimicking PHP 8.3 behavior.
if (\PHP_VERSION_ID < 80300) {
if (!isset($this->cachedExitCode) && !$running && -1 !== $this->processInformation['exitcode']) {
$this->cachedExitCode = $this->processInformation['exitcode'];
}

if (isset($this->cachedExitCode) && !$running && -1 === $this->processInformation['exitcode']) {
$this->processInformation['exitcode'] = $this->cachedExitCode;
}
}

$this->readPipes($running && $blocking, '\\' !== \DIRECTORY_SEPARATOR || !$running);

if ($this->fallbackStatus && $this->isSigchildEnabled()) {
Expand Down
55 changes: 54 additions & 1 deletion src/Symfony/Component/Process/Tests/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1541,6 +1541,60 @@ public function testEnvCaseInsensitiveOnWindows()
}
}

public function testMultipleCallsToProcGetStatus()
{
$process = $this->getProcess('echo foo');
$process->start(static function () use ($process) {
return $process->isRunning();
});
while ($process->isRunning()) {
usleep(1000);
}
$this->assertSame(0, $process->getExitCode());
}

public function testFailingProcessWithMultipleCallsToProcGetStatus()
{
$process = $this->getProcess('exit 123');
$process->start(static function () use ($process) {
return $process->isRunning();
});
while ($process->isRunning()) {
usleep(1000);
}
$this->assertSame(123, $process->getExitCode());
}

/**
* @group slow
*/
public function testLongRunningProcessWithMultipleCallsToProcGetStatus()
{
$process = $this->getProcess('php -r "sleep(1); echo \'done\';"');
$process->start(static function () use ($process) {
return $process->isRunning();
});
while ($process->isRunning()) {
usleep(1000);
}
$this->assertSame(0, $process->getExitCode());
}

/**
* @group slow
*/
public function testLongRunningProcessWithMultipleCallsToProcGetStatusError()
{
$process = $this->getProcess('php -r "sleep(1); echo \'failure\'; exit(123);"');
$process->start(static function () use ($process) {
return $process->isRunning();
});
while ($process->isRunning()) {
usleep(1000);
}
$this->assertSame(123, $process->getExitCode());
}

/**
* @group transient-on-windows
*/
Expand All @@ -1556,7 +1610,6 @@ public function testNotTerminableInputPipe()

/**
* @param string|array $commandline
* @param mixed $input
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't want to touch this but Fabbot code style tests asks to remove it. Bringing it up just for clarity.

*/
private function getProcess($commandline, ?string $cwd = null, ?array $env = null, $input = null, ?int $timeout = 60): Process
{
Expand Down