Skip to content

Process: fixed captured output corruption on Windows with PHP < 8.5 - #347

Open
TonnyJe wants to merge 1 commit into
nette:masterfrom
TonnyJe:process-temporary-file-handles
Open

TonnyJe wants to merge 1 commit into
nette:masterfrom
TonnyJe:process-temporary-file-handles

Conversation

@TonnyJe

@TonnyJe TonnyJe commented Nov 26, 2026

Copy link
Copy Markdown
  • bug fix / new feature? bug fix
  • BC break? no

Problem

On Windows with PHP < 8.5, output captured by Process (the default stdout/stderr) is truncated and corrupted
whenever 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

$p = Nette\Utils\Process::runExecutable(PHP_BINARY, ['-r', 'for ($i = 0; $i < 800; $i++) echo str_repeat("x", 65536);']);
var_dump(strlen($p->getStdOutput())); // expected 52428800

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 to proc_open(). The child inherits the same file object, so
the child and the parent share one file position: the parent's fseek() before each read moves the position the child
writes at, and the child overwrites output not read yet.

Solution

  • The child writes through its own handle of a named temporary file (fopen($path, 'xb'), random name in
    sys_get_temp_dir()); the parent reads through a second handle ('rb') sequentially, without fseek().
  • fopen() and unlink() go through Callback::invokeSafe(): a failure to create or open the file is
    ProcessFailedException, also with an application error handler that throws; cleanup never throws. Files created
    before a failure (also a failed proc_open()) are removed.
  • Right after the start the parent closes its copy of the writing handle and removes the file from the directory,
    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.md updated (it said tmpfile()).

Tests

Process.output.temporaryFile.phpt: exact binary content of both streams with incremental and final collection and
with 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

  • Before the change the new test fails on Windows PHP 8.2, 8.3, 8.4 and passes elsewhere (pipes, not affected).
  • After it all Process.* tests pass on every environment below; no process and no temporary file is left behind.
  • Whole suite (tester tests -s with the Tester from composer install, as in CI) on the same environments: no failure
    that does not occur on master as well (here: intl not loaded, file permissions of a WSL-mounted drive, the PHPStan
    version printing a type differently, an intermittent FileSystem.copy).
  • DressCode, Nette Code Checker (--strict-types) and PHPStan: no errors.
  • Environments: Linux PHP 8.2, 8.4, 8.5 (Docker php:*-cli); Windows 11 PHP 8.2, 8.3, 8.4, 8.5, 8.6 (official NTS
    builds), 8.5 ZTS and a PHP 8.5 development build.
  • CI of this branch on the fork: Coding Style and Static Analysis pass. Tests fail only in tests that fail on master
    as well: types/TypesTest.phpt on every job (a newer PHPStan prints (float|int); it passed on master's last CI run
    on 2026-11-06 and fails on master locally now) and Process.environment.phpt on Windows PHP 8.5 and 8.6 (fails in
    master's last CI run).
  • Note: the CI run of the separate branch with the piping fix, which does not contain this change, failed in the
    existing Process.basic.phpt "reads large output without deadlocking" on Windows PHP 8.4 with 802816 instead of
    1000000 bytes: this bug, caught by the existing test intermittently.

Known limitations

  • The immediate removal is best effort. A file that cannot be removed (an ACL, other software holding it without
    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.
  • A full disk truncates the output silently, as before (not tested).
  • The output is still not bounded in size; a child writing without a pause can make one read very large.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant