diff --git a/src/Utils/Process.php b/src/Utils/Process.php index 6a7831406..d7a490e3c 100644 --- a/src/Utils/Process.php +++ b/src/Utils/Process.php @@ -140,7 +140,9 @@ private function __construct( [$this->inputPipe, $this->outputPipes[self::StdOut], $this->outputPipes[self::StdErr]] = $pipes + $descriptors; if ($stdin instanceof self) { - // the source process hands over its STDOUT pipe; from now on this process owns it + // the source process hands over its STDOUT pipe: the child has its own copy of the read end, the parent's + // copy is closed, otherwise the source would not get a broken pipe when this process exits + fclose($this->inputPipe); unset( $stdin->outputBuffers[self::StdOut], $stdin->outputBufferOffsets[self::StdOut], diff --git a/tests/Utils/Process.piping.ownership.phpt b/tests/Utils/Process.piping.ownership.phpt new file mode 100644 index 000000000..3875f3110 --- /dev/null +++ b/tests/Utils/Process.piping.ownership.phpt @@ -0,0 +1,89 @@ + Process::runExecutable( + getPhpCliBinary(), + ['-r', '$s = str_repeat("x", 65536); for ($i = 0; $i < 1600; $i++) { if (@fwrite(STDOUT, $s) === false) exit(3); }'], // 100 MiB + timeout: $timeout, +); +$readFirst = fn(Process $source, int $bytes, mixed $stdout = false) => Process::runExecutable( + getPhpCliBinary(), + ['-r', "echo strlen(fread(STDIN, $bytes));"], + stdin: $source, + stdout: $stdout, + timeout: 10, +); + + +test('the source ends when the destination exits early', function () use ($writer, $readFirst) { + $source = $writer(); + $destination = $readFirst($source, 1000); + $start = hrtime(true); + Assert::same(3, $source->getExitCode()); // write failed: broken pipe + Assert::true((hrtime(true) - $start) / 1e9 < 5); + Assert::same(0, $destination->getExitCode()); +}); + + +test('every stage of a pipeline ends when the last one exits early', function () use ($writer, $readFirst) { + $a = $writer(); + $b = Process::runExecutable( + getPhpCliBinary(), + ['-r', 'while (($s = fread(STDIN, 65536)) !== "" && $s !== false) { if (@fwrite(STDOUT, $s) === false) exit(4); }'], + stdin: $a, + timeout: 10, + ); + $c = $readFirst($b, 1000); + $start = hrtime(true); + Assert::same(0, $c->getExitCode()); + Assert::same(4, $b->getExitCode()); + Assert::same(3, $a->getExitCode()); + Assert::true((hrtime(true) - $start) / 1e9 < 5); +}); + + +test('the destination cannot be written to and closing its STDIN does nothing', function () use ($writer, $readFirst) { + $source = $writer(); + $destination = $readFirst($source, 1000, null); + Assert::exception( + fn() => $destination->writeStdInput('x'), + Nette\InvalidStateException::class, + 'Cannot write to process: STDIN pipe is closed', + ); + $destination->closeStdInput(); + Assert::same('1000', $destination->getStdOutput()); + Assert::same(3, $source->getExitCode()); +}); + + +test('the source no longer captures its STDOUT', function () use ($writer, $readFirst) { + $source = $writer(); + $readFirst($source, 1000); + Assert::exception( + fn() => $source->consumeStdOutput(), + Nette\InvalidStateException::class, + 'Cannot read output: it is not captured %a%', + ); +}); + + +test('if the destination cannot be created, the source keeps its output', function () { + $source = Process::runExecutable(getPhpCliBinary(), ['-r', 'echo str_repeat("y", 1000);']); + Assert::exception( + fn() => Process::runExecutable(getPhpCliBinary(), ['-r', 'echo 1;'], stdin: $source, stdout: __DIR__ . '/nonexistent-dir/x.txt'), + Nette\IOException::class, + ); + Assert::same(str_repeat('y', 1000), $source->getStdOutput()); +});