diff --git a/src/Symfony/Component/Process/Process.php b/src/Symfony/Component/Process/Process.php index 7c4e925004dbf..7e6a77af1f0a2 100644 --- a/src/Symfony/Component/Process/Process.php +++ b/src/Symfony/Component/Process/Process.php @@ -1289,8 +1289,24 @@ protected function updateStatus(bool $blocking): void } $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 (version_compare(\PHP_VERSION, '8.3.0', '<')) { + static $cachedExitCode = null; + + if (null === $cachedExitCode && !$running && -1 !== $this->processInformation['exitcode']) { + $cachedExitCode = $this->processInformation['exitcode']; + } + + if (null !== $cachedExitCode && !$running && -1 === $this->processInformation['exitcode']) { + $this->processInformation['exitcode'] = $cachedExitCode; + } + } + $this->readPipes($running && $blocking, '\\' !== \DIRECTORY_SEPARATOR || !$running); if ($this->fallbackStatus && $this->isSigchildEnabled()) {