From efc9cc58903cd70c29978e216f3f294d91a8a0b6 Mon Sep 17 00:00:00 2001 From: Nicolas Joubert Date: Wed, 4 Nov 2026 16:00:36 +0100 Subject: [PATCH 1/2] #459 - process_launcher: stop the sub-processes together Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 3 ++ docs/reference/tasks/process_launcher_task.md | 3 ++ src/Task/Process/ProcessLauncherTask.php | 52 +++++++++++++++---- .../Task/Process/ProcessLauncherTaskTest.php | 33 ++++++++++++ 4 files changed, 80 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ddd25e98..6eefdddc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,9 @@ Latest * [#447](https://github.com/cleverage/process-bundle/issues/447) `date_parser`: the trailing data allowed by a `+` in the format (e.g. `!Y-m-d+`) is no longer rejected (`Trailing data` warning, rejected since 5.7 with the other warnings). Update documentation, add tests. * [#449](https://github.com/cleverage/process-bundle/issues/449) `sprintf`: a width or a precision given as an argument (`%*d`, `%.*f`, `%1$*2$d`) is accepted by the validation of the `format` option (rejected since 5.7). Add tests. +### Fixes +* [#459](https://github.com/cleverage/process-bundle/issues/459) `process_launcher`: the sub-processes still running are stopped together (`SIGTERM` to all of them, `SIGKILL` after 5 seconds), instead of one after the other with up to 5 seconds each (40 seconds for 8 sub-processes, longer than the grace period of `docker stop` or Kubernetes). Update documentation, add tests. + v5.8 ----- diff --git a/docs/reference/tasks/process_launcher_task.md b/docs/reference/tasks/process_launcher_task.md index 9bd641c4..6b4a5903 100644 --- a/docs/reference/tasks/process_launcher_task.md +++ b/docs/reference/tasks/process_launcher_task.md @@ -38,6 +38,9 @@ When the process fails or is stopped (stop request, [StopTask](stop_task.md)) wh they are stopped when the task is finalized (a warning is logged), and the JSON stream files not output yet are removed: the sub-processes no longer go on with their side effects after the end of the process. +The sub-processes are stopped together: `SIGTERM` is sent to all of them, then `SIGKILL` to the ones still running +after 5 seconds, whatever their number (within the grace period of `docker stop` or Kubernetes). + Options ------- diff --git a/src/Task/Process/ProcessLauncherTask.php b/src/Task/Process/ProcessLauncherTask.php index ecbfbb94..b775ea35 100644 --- a/src/Task/Process/ProcessLauncherTask.php +++ b/src/Task/Process/ProcessLauncherTask.php @@ -36,6 +36,12 @@ */ class ProcessLauncherTask extends AbstractConfigurableTask implements FlushableTaskInterface, IterableTaskInterface, FinalizableTaskInterface { + /** Seconds given to the sub-processes to stop after SIGTERM, before SIGKILL */ + protected const STOP_TIMEOUT = 5; + + /** The value of SIGTERM, whose constant is defined by the pcntl extension */ + private const SIGTERM = 15; + /** * @var SubprocessInstance[] */ @@ -147,12 +153,9 @@ public function next(ProcessState $state): bool */ public function finalize(ProcessState $state): void { - $running = 0; + $running = \count(array_filter($this->launchedProcesses, static fn (SubprocessInstance $process): bool => !$process->getProcess()->isTerminated())); + $this->stopProcesses($this->launchedProcesses); foreach ($this->launchedProcesses as $process) { - if (!$process->getProcess()->isTerminated()) { - ++$running; - } - $process->stop(5); $process->removeBuffer(); } if ($running > 0) { @@ -302,13 +305,40 @@ static function (Options $options, array $value): array { */ protected function killProcesses(): void { - foreach ($this->launchedProcesses as $process) { - // An already terminated sub-process keeps its buffer: it is handled normally at the next call - $running = !$process->getProcess()->isTerminated(); - $process->stop(5); - if ($running) { - $process->removeBuffer(); + // An already terminated sub-process keeps its buffer: it is handled normally at the next call + $running = array_filter($this->launchedProcesses, static fn (SubprocessInstance $process): bool => !$process->getProcess()->isTerminated()); + $this->stopProcesses($running); + foreach ($running as $process) { + $process->removeBuffer(); + } + } + + /** + * Stops the sub-processes together: SIGTERM to all of them, then SIGKILL to the ones still running after the + * timeout. Stopped one after the other, each one waiting up to the timeout, 8 sub-processes took up to 40 seconds, + * longer than the grace period of docker stop (10 s) or Kubernetes (30 s). + * + * @param array $processes + */ + protected function stopProcesses(array $processes, float $timeout = self::STOP_TIMEOUT): void + { + foreach ($processes as $process) { + if ($process->getProcess()->isRunning()) { + try { + $process->getProcess()->signal(self::SIGTERM); + } catch (\Throwable) { + // Terminated in the meantime, or a signal not supported (Windows): stopped below + } } } + $deadline = microtime(true) + $timeout; + while (microtime(true) < $deadline + && [] !== array_filter($processes, static fn (SubprocessInstance $process): bool => $process->getProcess()->isRunning())) { + usleep(10_000); + } + foreach ($processes as $process) { + // SIGKILL if still running + $process->stop(0); + } } } diff --git a/tests/Task/Process/ProcessLauncherTaskTest.php b/tests/Task/Process/ProcessLauncherTaskTest.php index 828b1cde..60e2d6af 100644 --- a/tests/Task/Process/ProcessLauncherTaskTest.php +++ b/tests/Task/Process/ProcessLauncherTaskTest.php @@ -23,6 +23,7 @@ use CleverAge\ProcessBundle\Model\SubprocessInstance; use CleverAge\ProcessBundle\Registry\ProcessConfigurationRegistry; use CleverAge\ProcessBundle\Task\Process\ProcessLauncherTask; +use PHPUnit\Framework\Attributes\RequiresPhpExtension; use PHPUnit\Framework\TestCase; use Psr\Log\NullLogger; use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; @@ -220,6 +221,33 @@ public function testFinishedBuffersNotOutputAreRemovedWhenTheTaskIsFinalized(): $this->assertFalse($task->next($state)); } + /** + * Stopped one after the other, each one waiting up to the timeout, 3 sub-processes ignoring SIGTERM took 3 times + * the timeout. + */ + #[RequiresPhpExtension('pcntl')] + public function testTheSubprocessesAreStoppedTogether(): void + { + [$task, $state] = $this->launch('stubborn', wait: false); + $this->launch('stubborn', $task, $state, wait: false); + $this->launch('stubborn', $task, $state, wait: false); + /** @var array $launchedProcesses */ + $launchedProcesses = (new \ReflectionProperty($task, 'launchedProcesses'))->getValue($task); + $this->assertCount(3, $launchedProcesses); + usleep(300_000); // The sub-processes ignore SIGTERM + + $start = microtime(true); + (new \ReflectionMethod($task, 'stopProcesses'))->invoke($task, $launchedProcesses, 1.0); + $duration = microtime(true) - $start; + + foreach ($launchedProcesses as $process) { + $this->assertFalse($process->getProcess()->isRunning()); + $process->removeBuffer(); + } + $this->assertGreaterThanOrEqual(1.0, $duration, 'Killed after the timeout'); + $this->assertLessThan(2.0, $duration, 'A single timeout for all of them'); + } + public function testFinalizeWithoutSubprocess(): void { $task = $this->createTask(); @@ -261,6 +289,11 @@ private function launch(string $input, ?ProcessLauncherTask $task = null, ?Proce exit(1); } elseif ('sleep' === $input) { sleep(30); + } elseif ('stubborn' === $input) { + // Ignores SIGTERM: killed after the stop timeout + pcntl_async_signals(true); + pcntl_signal(15, SIG_IGN); + sleep(30); } PHP); } From 32c675f5dbfb211b8859e946fb03bfb4bfbadd67 Mon Sep 17 00:00:00 2001 From: Nicolas Joubert Date: Wed, 4 Nov 2026 19:23:32 +0100 Subject: [PATCH 2/2] #459 - Fix the section of the CHANGELOG entry Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6eefdddc..4b318fe1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Latest * [#440](https://github.com/cleverage/process-bundle/issues/440) Documentation: `json_stream_reader` and `file_splitter` open their path with `SplFileObject`, any PHP stream wrapper is accepted (`http://`, `php://filter`...); the path must not be built from untrusted input (SSRF, reading local files), validate it before the task. ## Fixes +* [#459](https://github.com/cleverage/process-bundle/issues/459) `process_launcher`: the sub-processes still running are stopped together (`SIGTERM` to all of them, `SIGKILL` after 5 seconds), instead of one after the other with up to 5 seconds each (40 seconds for 8 sub-processes, longer than the grace period of `docker stop` or Kubernetes). Update documentation, add tests. * [#455](https://github.com/cleverage/process-bundle/issues/455) `cleverage:process:execute -o -t json-stream`: the invalid UTF-8 sequences of the output are replaced, as on STDOUT (a `JsonException` was thrown, also by the buffer of `process_launcher`); an output that cannot be encoded as JSON is reported on the error output. Update documentation, add tests. * [#457](https://github.com/cleverage/process-bundle/issues/457) `mapping` / `callback`: the deprecations of a literal key of an array destination (once per target property) and of `additional_parameters` are triggered once, instead of for each item or each resolution of the options. Add tests. * [#453](https://github.com/cleverage/process-bundle/issues/453) `trim`: the ranges of single-byte characters (`a..z`) of a `charlist` with a multibyte character are expanded before `mb_trim()`, which does not support them (the range was lost, its `.` removed). Update documentation, add tests. @@ -17,9 +18,6 @@ Latest * [#447](https://github.com/cleverage/process-bundle/issues/447) `date_parser`: the trailing data allowed by a `+` in the format (e.g. `!Y-m-d+`) is no longer rejected (`Trailing data` warning, rejected since 5.7 with the other warnings). Update documentation, add tests. * [#449](https://github.com/cleverage/process-bundle/issues/449) `sprintf`: a width or a precision given as an argument (`%*d`, `%.*f`, `%1$*2$d`) is accepted by the validation of the `format` option (rejected since 5.7). Add tests. -### Fixes -* [#459](https://github.com/cleverage/process-bundle/issues/459) `process_launcher`: the sub-processes still running are stopped together (`SIGTERM` to all of them, `SIGKILL` after 5 seconds), instead of one after the other with up to 5 seconds each (40 seconds for 8 sub-processes, longer than the grace period of `docker stop` or Kubernetes). Update documentation, add tests. - v5.8 -----