Skip to content

[Console] #47809 remove exit() call in last SignalHandler #48299

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

Merged
merged 1 commit into from
Dec 9, 2022
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
9 changes: 1 addition & 8 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -998,15 +998,8 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
foreach ($this->signalsToDispatchEvent as $signal) {
$event = new ConsoleSignalEvent($command, $input, $output, $signal);

$this->signalRegistry->register($signal, function ($signal, $hasNext) use ($event) {
$this->signalRegistry->register($signal, function () use ($event) {
$this->dispatcher->dispatch($event, ConsoleEvents::SIGNAL);

// No more handlers, we try to simulate PHP default behavior
if (!$hasNext) {
if (!\in_array($signal, [\SIGUSR1, \SIGUSR2], true)) {
exit(0);
}
}
});
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.3
---

* Remove `exit` call in `Application` signal handlers. Commands will no longer be automatically interrupted after receiving signal other than `SIGUSR1` or `SIGUSR2`

6.2
---

Expand Down
40 changes: 37 additions & 3 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2014,6 +2014,19 @@ public function testSignalableCommandHandlerCalledAfterEventListener()
$this->assertSame([SignalEventSubscriber::class, SignableCommand::class], $command->signalHandlers);
}

public function testSignalableCommandDoesNotInterruptedOnTermSignals()
{
$command = new TerminatableCommand(true, \SIGINT);
$command->exitCode = 129;

$dispatcher = new EventDispatcher();
$application = new Application();
$application->setAutoExit(false);
$application->setDispatcher($dispatcher);
$application->add($command);
$this->assertSame(129, $application->run(new ArrayInput(['signal'])));
}

/**
* @group tty
*/
Expand Down Expand Up @@ -2113,26 +2126,31 @@ public function isEnabled(): bool
class BaseSignableCommand extends Command
{
public $signaled = false;
public $exitCode = 1;
public $signalHandlers = [];
public $loop = 1000;
private $emitsSignal;
private $signal;

public function __construct(bool $emitsSignal = true)
protected static $defaultName = 'signal';

public function __construct(bool $emitsSignal = true, int $signal = \SIGUSR1)
{
parent::__construct();
$this->emitsSignal = $emitsSignal;
$this->signal = $signal;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
if ($this->emitsSignal) {
posix_kill(posix_getpid(), \SIGUSR1);
posix_kill(posix_getpid(), $this->signal);
}

for ($i = 0; $i < $this->loop; ++$i) {
usleep(100);
if ($this->signaled) {
return 1;
return $this->exitCode;
}
}

Expand All @@ -2155,6 +2173,22 @@ public function handleSignal(int $signal): void
}
}

class TerminatableCommand extends BaseSignableCommand implements SignalableCommandInterface
{
protected static $defaultName = 'signal';

public function getSubscribedSignals(): array
{
return SignalRegistry::isSupported() ? [\SIGINT] : [];
}

public function handleSignal(int $signal): void
{
$this->signaled = true;
$this->signalHandlers[] = __CLASS__;
}
}

class SignalEventSubscriber implements EventSubscriberInterface
{
public $signaled = false;
Expand Down