10000 [Console] remove application exit on last signal handler · symfony/symfony@bbf315e · GitHub
[go: up one dir, main page]

Skip to content

Commit bbf315e

Browse files
committed
[Console] remove application exit on last signal handler
1 parent ddcf1ab commit bbf315e

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,15 +1018,8 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
10181018
foreach ($this->signalsToDispatchEvent as $signal) {
10191019
$event = new ConsoleSignalEvent($command, $input, $output, $signal);
10201020

1021-
$this->signalRegistry->register($signal, function ($signal, $hasNext) use ($event) {
1021+
$this->signalRegistry->register($signal, function () use ($event) {
10221022
$this->dispatcher->dispatch($event, ConsoleEvents::SIGNAL);
1023-
1024-
// No more handlers, we try to simulate PHP default behavior
1025-
if (!$hasNext) {
1026-
if (!\in_array($signal, [\SIGUSR1, \SIGUSR2], true)) {
1027-
exit(0);
1028-
}
1029-
}
10301023
});
10311024
}
10321025
}

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -509,12 +509,12 @@ public function testDontRunAlternativeNamespaceName()
509509
$tester = new ApplicationTester($application);
510510
$tester->run(['command' => 'foos:bar1'], ['decorated' => false]);
511511
$this->assertSame('
512-
513-
There are no commands defined in the "foos" namespace.
514-
515-
Did you mean this?
516-
foo
517-
512+
513+
There are no commands defined in the "foos" namespace.
514+
515+
Did you mean this?
516+
foo
517+
518518
519519
', $tester->getDisplay(true));
520520
}
@@ -1990,6 +1990,19 @@ public function testSignalableCommandHandlerCalledAfterEventListener()
19901990
$this->assertSame([SignalEventSubscriber::class, SignableCommand::class], $command->signalHandlers);
19911991
}
19921992

1993+
public function testSignalableCommandDoesNotInterruptedOnTermSignals()
1994+
{
1995+
$command = new TerminatableCommand(true, \SIGINT);
1996+
$command->exitCode = 129;
1997+
1998+
$dispatcher = new EventDispatcher();
1999+
$application = new Application();
2000+
$application->setAutoExit(false);
2001+
$application->setDispatcher($dispatcher);
2002+
$application->add($command);
2003+
$this->assertSame(129, $application->run(new ArrayInput(['signal'])));
2004+
}
2005+
19932006
/**
19942007
* @group tty
19952008
*/
@@ -2091,28 +2104,30 @@ public function isEnabled(): bool
20912104
class BaseSignableCommand extends Command
20922105
{
20932106
public $signaled = false;
2107+
public $exitCode = 1;
20942108
public $signalHandlers = [];
20952109
public $loop = 1000;
20962110
private $emitsSignal;
2111+
private $signal;
20972112

20982113
protected static $defaultName = 'signal';
20992114

2100-
public function __construct(bool $emitsSignal = true)
2101-
{
2115+
public function __construct(bool $emitsSignal = true, int $signal = \SIGUSR1) {
21022116
parent::__construct();
21032117
$this->emitsSignal = $emitsSignal;
2118+
$this->signal = $signal;
21042119
}
21052120

21062121
protected function execute(InputInterface $input, OutputInterface $output): int
21072122
{
21082123
if ($this->emitsSignal) {
2109-
posix_kill(posix_getpid(), SIGUSR1);
2124+
posix_kill(posix_getpid(), $this->signal);
21102125
}
21112126

21122127
for ($i = 0; $i < $this->loop; ++$i) {
21132128
usleep(100);
21142129
if ($this->signaled) {
2115-
return 1;
2130+
return $this->exitCode;
21162131
}
21172132
}
21182133

@@ -2136,6 +2151,22 @@ public function handleSignal(int $signal): void
21362151
}
21372152
}
21382153

2154+
class TerminatableCommand extends BaseSignableCommand implements SignalableCommandInterface
2155+
{
2156+
protected static $defaultName = 'signal';
2157+
2158+
public function getSubscribedSignals(): array
2159+
{
2160+
return SignalRegistry::isSupported() ? [\SIGINT] : [];
2161+
}
2162+
2163+
public function handleSignal(int $signal): void
2164+
{
2165+
$this->signaled = true;
2166+
$this->signalHandlers[] = __CLASS__;
2167+
}
2168+
}
2169+
21392170
class SignalEventSubscriber implements EventSubscriberInterface
21402171
{
21412172
public $signaled = false;

0 commit comments

Comments
 (0)
0