Skip to content

Process: fixed pipe resource leak when chaining processes - #348

Open
TonnyJe wants to merge 1 commit into
nette:masterfrom
TonnyJe:process-pipe-read-end
Open

TonnyJe wants to merge 1 commit into
nette:masterfrom
TonnyJe:process-pipe-read-end

Conversation

@TonnyJe

@TonnyJe TonnyJe commented Nov 26, 2026

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

Problem

When a Process is passed as stdin of another one and the destination exits early (it needs only the first part of
the input), the source does not get a broken pipe. Waiting for the source then blocks until its timeout, unless the
caller happens to wait for the destination first.

Reproduction

use Nette\Utils\Process;

$source = Process::runExecutable(PHP_BINARY, ['-r', 'while (@fwrite(STDOUT, str_repeat("x", 65536)) !== false);'], timeout: 10);
$destination = Process::runExecutable(PHP_BINARY, ['-r', 'echo strlen(fread(STDIN, 1000));'], stdin: $source);
$source->wait(); // ProcessTimeoutException after 10 s; expected: the source ends at once on a broken pipe

Linux, PHP 8.2 and 8.5: ProcessTimeoutException after 10.0 s; with the fix the source exits in 0.01 s.

Root cause

The source's STDOUT pipe is passed to proc_open() of the destination. PHP duplicates it for the child and leaves the
original open. Process kept that original as the destination's $inputPipe ($pipes + $descriptors, there is no
$pipes[0] for a resource descriptor), so the parent held a read end of the pipe until the destination was closed
(when a wait for it saw it finish, or when the object was destroyed).

Solution

The parent's copy is closed right after proc_open(), the child has its own. writeStdInput() on such a destination
throws InvalidStateException as for a closed input (before, it wrote into a read end); closeStdInput() does nothing.

Tests

Process.piping.ownership.phpt: an early exit of the destination (the source exits on its write error within seconds;
before: ProcessTimeoutException after 10 s); a three-stage pipeline whose last stage exits early; STDIN of the
destination; the source no longer captures its STDOUT; a destination that fails to start leaves the source's output
intact. Skipped on Windows, where piping throws NotSupportedException.

Verified

  • Before the change the new test fails on Linux PHP 8.2, 8.4, 8.5 (ProcessTimeoutException after 10 s); it is skipped
    on Windows.
  • After it all Process.* tests pass on every environment below; no process 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).

Known limitations

  • POSIX only, as piping itself.
  • A source that ignores write errors (or SIGPIPE) still runs until its own end or its timeout; it is just no longer
    kept blocked by the parent.

The destination kept the source's STDOUT pipe as its own input pipe
after proc_open(), so the parent still held a read end of the pipe
until the destination was closed. When the destination exited early,
the source did not get a broken pipe, and waiting for the source
blocked until its timeout. The parent's copy is now closed once the
destination has inherited the pipe.
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