From 6be1c17bdc511122b1100954374681252c6b851b Mon Sep 17 00:00:00 2001 From: matt Date: Wed, 12 Nov 2025 21:58:07 +0000 Subject: [PATCH 1/4] Add failing test for ShutdownHandler with child process --- composer.json | 3 ++ tests/end-to-end/_files/ChildProcessTest.php | 30 +++++++++++++++++++ .../end-to-end/cli/child-process-output.phpt | 23 ++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 tests/end-to-end/_files/ChildProcessTest.php create mode 100644 tests/end-to-end/cli/child-process-output.phpt diff --git a/composer.json b/composer.json index 4d443951aa..ab3171616d 100644 --- a/composer.json +++ b/composer.json @@ -48,6 +48,9 @@ "sebastian/version": "^6.0.0", "staabm/side-effects-detector": "^1.0.5" }, + "require-dev": { + "ext-pcntl": "*" + }, "config": { "platform": { "php": "8.3.0" diff --git a/tests/end-to-end/_files/ChildProcessTest.php b/tests/end-to-end/_files/ChildProcessTest.php new file mode 100644 index 0000000000..bf8270c819 --- /dev/null +++ b/tests/end-to-end/_files/ChildProcessTest.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PHPUnit\TestFixture; + +use function pcntl_fork; +use function pcntl_wait; +use PHPUnit\Framework\TestCase; + +final class ChildProcessTest extends TestCase +{ + public function testChildProcessOutput(): void + { + $child = pcntl_fork(); + $this->assertGreaterThan(-1, $child); + + if ($child) { + pcntl_wait($child); + $this->assertTrue(true); + } else { + exit(0); + } + } +} diff --git a/tests/end-to-end/cli/child-process-output.phpt b/tests/end-to-end/cli/child-process-output.phpt new file mode 100644 index 0000000000..d85e4a1c44 --- /dev/null +++ b/tests/end-to-end/cli/child-process-output.phpt @@ -0,0 +1,23 @@ +--TEST-- +ShutdownHandler does not output when child process exits +--FILE-- +run($_SERVER['argv']); +--EXPECTF-- +PHPUnit %s by Sebastian Bergmann and contributors. + +Runtime: %s + +. 1 / 1 (100%) + +Time: %s, Memory: %s + +OK (1 test, 2 assertions) From 0699696cc9d7dbb11a1f2e026a01ebec06c1917d Mon Sep 17 00:00:00 2001 From: matt Date: Wed, 12 Nov 2025 22:06:34 +0000 Subject: [PATCH 2/4] ShutdownHandler does not output in child processes --- src/Runner/ShutdownHandler.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Runner/ShutdownHandler.php b/src/Runner/ShutdownHandler.php index faecf088c3..129ec9f868 100644 --- a/src/Runner/ShutdownHandler.php +++ b/src/Runner/ShutdownHandler.php @@ -10,6 +10,7 @@ namespace PHPUnit\Runner; use const PHP_EOL; +use function getmypid; use function register_shutdown_function; use function rtrim; @@ -42,13 +43,14 @@ private static function register(): void } self::$registered = true; + $pid = getmypid(); register_shutdown_function( - static function (): void + static function () use ($pid): void { $message = rtrim(self::$message); - if ($message === '') { + if ($message === '' || $pid !== getmypid()) { return; } From 69022c73bcab30694cdda42df7e59348778cd90c Mon Sep 17 00:00:00 2001 From: matt Date: Thu, 13 Nov 2025 09:25:00 +0000 Subject: [PATCH 3/4] Skip test if pcntl not available, remove require-dev --- composer.json | 3 --- tests/end-to-end/cli/child-process-output.phpt | 5 +++++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index ab3171616d..4d443951aa 100644 --- a/composer.json +++ b/composer.json @@ -48,9 +48,6 @@ "sebastian/version": "^6.0.0", "staabm/side-effects-detector": "^1.0.5" }, - "require-dev": { - "ext-pcntl": "*" - }, "config": { "platform": { "php": "8.3.0" diff --git a/tests/end-to-end/cli/child-process-output.phpt b/tests/end-to-end/cli/child-process-output.phpt index d85e4a1c44..ca0edad990 100644 --- a/tests/end-to-end/cli/child-process-output.phpt +++ b/tests/end-to-end/cli/child-process-output.phpt @@ -1,5 +1,10 @@ --TEST-- ShutdownHandler does not output when child process exits +--SKIPIF-- + Date: Thu, 13 Nov 2025 09:27:00 +0000 Subject: [PATCH 4/4] Remove unnecessary filter --- tests/end-to-end/cli/child-process-output.phpt | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/end-to-end/cli/child-process-output.phpt b/tests/end-to-end/cli/child-process-output.phpt index ca0edad990..3624916670 100644 --- a/tests/end-to-end/cli/child-process-output.phpt +++ b/tests/end-to-end/cli/child-process-output.phpt @@ -9,8 +9,6 @@ if (!extension_loaded('pcntl')) {