Skip to content

Commit

Permalink
Merge branch '5.4' into 6.4
Browse files Browse the repository at this point in the history
* 5.4:
  Update configuration path in help message
  [Validator] Review Albanian translation
  [Process] Fix Inconsistent Exit Status in proc_get_status for PHP Versions Below 8.3
  [Validator] Update Czech (cz) translation
  Sync translations
  Review portuguese translations
  [Validator] Fix fields without constraints in `Collection`
  deal with fields for which no constraints have been configured
  • Loading branch information
derrabus committed Feb 12, 2024
2 parents 31642b0 + 7e2c857 commit d4fd4a8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class Process implements \IteratorAggregate
private WindowsPipes|UnixPipes $processPipes;

private ?int $latestSignal = null;
private ?int $cachedExitCode = null;

private static ?bool $sigchild = null;

Expand Down Expand Up @@ -1291,6 +1292,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
54 changes: 54 additions & 0 deletions Tests/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,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 Down

0 comments on commit d4fd4a8

Please sign in to comment.