Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <file> -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.
Expand Down
3 changes: 3 additions & 0 deletions docs/reference/tasks/process_launcher_task.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------

Expand Down
52 changes: 41 additions & 11 deletions src/Task/Process/ProcessLauncherTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
*/
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<SubprocessInstance> $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);
}
}
}
33 changes: 33 additions & 0 deletions tests/Task/Process/ProcessLauncherTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<SubprocessInstance> $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();
Expand Down Expand Up @@ -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);
}
Expand Down
Loading