Repository navigation
Conversation
On Windows with PHP < 8.5 the captured output goes through a tmpfile() handle inherited by the child. The handle shares the file position with the parent, so the parent's fseek() before each read moved the position the child wrote at, and the child overwrote output not read yet: the output was truncated and corrupted whenever the child wrote while the parent read. The child now writes through its own handle of a named temporary file and the parent reads through another one. Failures to create or open the file throw ProcessFailedException without involving the application's error handler; files created before a failure are removed. The file is removed from the directory right after the start, best effort. If the removal fails, close() and the destructor retry; a file that still cannot be removed stays in the temporary directory.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On Windows with PHP < 8.5, output captured by
Process(the defaultstdout/stderr) is truncated and corruptedwhenever the child writes while the parent reads it (
wait(),isRunning(),consumeStdOutput(),getStdOutput()), without any error. PHP ≥ 8.5 and other platforms use pipes and are not affected.Reproduction
Windows 11, PHP 8.2 and 8.4: 32–42 MB with a different hash in every run; with the fix exactly 52428800. A child writing 2000 lines of 100 bytes 1 ms
apart lost 3–18 lines in 5 of 5 runs. Output written before the first read is not affected.
Root cause
On Windows < 8.5 the output goes to a
tmpfile()passed toproc_open(). The child inherits the same file object, sothe child and the parent share one file position: the parent's
fseek()before each read moves the position the childwrites at, and the child overwrites output not read yet.
Solution
fopen($path, 'xb'), random name insys_get_temp_dir()); the parent reads through a second handle ('rb') sequentially, withoutfseek().fopen()andunlink()go throughCallback::invokeSafe(): a failure to create or open the file isProcessFailedException, also with an application error handler that throws; cleanup never throws. Files createdbefore a failure (also a failed
proc_open()) are removed.best effort: where the platform allows removing an open file, the handles keep working. If the removal fails,
close()and the destructor retry.docs/internals.mdupdated (it saidtmpfile()).Tests
Process.output.temporaryFile.phpt: exact binary content of both streams with incremental and final collection andwith a callback; repeated reads after the reader reached the current end of a growing output; temporary files removed
after normal use, after a failed start, after a failure to open a later output, with a descendant holding the output,
and not visible while the process runs; with an error handler that throws: creation denied by an ACL (
icacls)reported as
ProcessFailedException, removal denied by an ACL leaves the files without an error (cleaned by the test).The test uses its own temporary directory (
getTempDir()), as it checks what is left there.Verified
Process.*tests pass on every environment below; no process and no temporary file is left behind.tester tests -swith the Tester fromcomposer install, as in CI) on the same environments: no failurethat does not occur on master as well (here:
intlnot loaded, file permissions of a WSL-mounted drive, the PHPStanversion printing a type differently, an intermittent
FileSystem.copy).--strict-types) and PHPStan: no errors.php:*-cli); Windows 11 PHP 8.2, 8.3, 8.4, 8.5, 8.6 (official NTSbuilds), 8.5 ZTS and a PHP 8.5 development build.
as well:
types/TypesTest.phpton every job (a newer PHPStan prints(float|int); it passed on master's last CI runon 2026-11-06 and fails on master locally now) and
Process.environment.phpton Windows PHP 8.5 and 8.6 (fails inmaster's last CI run).
existing
Process.basic.phpt"reads large output without deadlocking" on Windows PHP 8.4 with 802816 instead of1000000 bytes: this bug, caught by the existing test intermittently.
Known limitations
sharing deletion) stays in the temporary directory; older Windows versions without POSIX delete semantics keep a
delete-pending name until the last handle closes (not tested). In one run in which PHP hit its memory limit, 3 such
files were listed in the temporary directory right afterwards and gone a few seconds later; not reproduced.