Skip to content

PHPUnit process is blocked when there's a lot of output and a test with separate process #6003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: 10.5
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 59 additions & 8 deletions src/Util/PHP/DefaultPhpProcess.php
Original file line number Diff line number Diff line change
@@ -12,12 +12,15 @@
use function array_merge;
use function fclose;
use function file_put_contents;
use function fread;
use function fwrite;
use function is_array;
use function is_resource;
use function proc_close;
use function proc_open;
use function stream_get_contents;
use function proc_terminate;
use function sprintf;
use function stream_select;
use function sys_get_temp_dir;
use function tempnam;
use function unlink;
@@ -112,16 +115,64 @@ protected function runProcess(string $job, array $settings): array

$stderr = $stdout = '';

if (isset($pipes[1])) {
$stdout = stream_get_contents($pipes[1]);
unset($pipes[0]);
$timeout = 5;

fclose($pipes[1]);
}
while (true) {
$r = $pipes;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$r = $pipes;
$readPipes = $pipes;

$w = null;
$e = null;

$n = @stream_select($r, $w, $e, $timeout);

if ($n === false) {
break;
}

if ($n === 0) {
proc_terminate($process, 9);

throw new PhpProcessException(
sprintf(
'Job execution aborted after %d seconds',
$timeout,
),
);
}

if (isset($pipes[2])) {
$stderr = stream_get_contents($pipes[2]);
if ($n > 0) {
foreach ($r as $pipe) {
$pipeOffset = 0;

fclose($pipes[2]);
foreach ($pipes as $i => $origPipe) {
if ($pipe === $origPipe) {
$pipeOffset = $i;

break;
}
}

if (!$pipeOffset) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!$pipeOffset) {
if ($pipeOffset === 0) {

break;
}

$line = fread($pipe, 8192);

if ($line === '' || $line === false) {
fclose($pipes[$pipeOffset]);

unset($pipes[$pipeOffset]);
} elseif ($pipeOffset === 1) {
$stdout .= $line;
} else {
$stderr .= $line;
}
}

if (empty($pipes)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (empty($pipes)) {
if ($pipes === []) {

break;
}
}
}

proc_close($process);
14 changes: 14 additions & 0 deletions tests/end-to-end/regression/5993.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
https://github.com/sebastianbergmann/phpunit/issues/5993
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--no-configuration';
$_SERVER['argv'][] = '--debug';
$_SERVER['argv'][] = __DIR__ . '/5993/Issue5993Test.php';

require_once __DIR__ . '/../../bootstrap.php';

(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
%A
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect the exact exception asserted here.

29 changes: 29 additions & 0 deletions tests/end-to-end/regression/5993/Issue5993Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--TEST--
PHPUnit process is blocked when there's a lot of output and a test with separate process
--INI--
error_reporting=-1
display_errors=1
display_startup_errors=1
memory_limit=-1
zend.assertions=1
assert.exception=1
--SKIPIF--
<?php
for ($i = 0; $i < 390; $i++) {
\trigger_error("error {$i}");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are all the inis needed? Isn't standard long fwrite to stderr/stdout enough?

Copy link
Contributor Author

@staabm staabm Oct 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you want to assist in fixing this problem, you could try to reduce the reproducer to a very minimum.

we suppose the problem is a underlying php-src bug and having a reproducer small enough to report to the php-src project would be valuable - so at best getting something which no longer depends on PHPUnit.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the main or the child blocked?

If the main (reading) process, then output to long stdout/stderr must be enough.

If the child process, then I do not understand the problem.

Copy link
Contributor Author

@staabm staabm Oct 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think 3 processes are involved. Needs further investigation

phpunit main -> PHPT --TEST-- isolation -> process isolation (because of the attribute)

}
?>
--FILE--
<?php

use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use PHPUnit\Framework\TestCase;

final class Issue5993Test extends TestCase
{
#[RunInSeparateProcess]
public function testOne(): void
{
$this->assertTrue(true);
}
}
Loading