Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bb7779d

Browse filesBrowse files
akuziafabpot
authored andcommitted
[Console] #47809 remove exit() call in last SignalHandler
1 parent 13059f6 commit bb7779d
Copy full SHA for bb7779d

File tree

3 files changed

+43
-11
lines changed
Filter options

3 files changed

+43
-11
lines changed

‎src/Symfony/Component/Console/Application.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Application.php
+1-8Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -998,15 +998,8 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
998998
foreach ($this->signalsToDispatchEvent as $signal) {
999999
$event = new ConsoleSignalEvent($command, $input, $output, $signal);
10001000

1001-
$this->signalRegistry->register($signal, function ($signal, $hasNext) use ($event) {
1001+
$this->signalRegistry->register($signal, function () use ($event) {
10021002
$this->dispatcher->dispatch($event, ConsoleEvents::SIGNAL);
1003-
1004-
// No more handlers, we try to simulate PHP default behavior
1005-
if (!$hasNext) {
1006-
if (!\in_array($signal, [\SIGUSR1, \SIGUSR2], true)) {
1007-
exit(0);
1008-
}
1009-
}
10101003
});
10111004
}
10121005
}

‎src/Symfony/Component/Console/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.3
5+
---
6+
7+
* Remove `exit` call in `Application` signal handlers. Commands will no longer be automatically interrupted after receiving signal other than `SIGUSR1` or `SIGUSR2`
8+
49
6.2
510
---
611

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/ApplicationTest.php
+37-3Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,6 +2014,19 @@ public function testSignalableCommandHandlerCalledAfterEventListener()
20142014
$this->assertSame([SignalEventSubscriber::class, SignableCommand::class], $command->signalHandlers);
20152015
}
20162016

2017+
public function testSignalableCommandDoesNotInterruptedOnTermSignals()
2018+
{
2019+
$command = new TerminatableCommand(true, \SIGINT);
2020+
$command->exitCode = 129;
2021+
2022+
$dispatcher = new EventDispatcher();
2023+
$application = new Application();
2024+
$application->setAutoExit(false);
2025+
$application->setDispatcher($dispatcher);
2026+
$application->add($command);
2027+
$this->assertSame(129, $application->run(new ArrayInput(['signal'])));
2028+
}
2029+
20172030
/**
20182031
* @group tty
20192032
*/
@@ -2113,26 +2126,31 @@ public function isEnabled(): bool
21132126
class BaseSignableCommand extends Command
21142127
{
21152128
public $signaled = false;
2129+
public $exitCode = 1;
21162130
public $signalHandlers = [];
21172131
public $loop = 1000;
21182132
private $emitsSignal;
2133+
private $signal;
21192134

2120-
public function __construct(bool $emitsSignal = true)
2135+
protected static $defaultName = 'signal';
2136+
2137+
public function __construct(bool $emitsSignal = true, int $signal = \SIGUSR1)
21212138
{
21222139
parent::__construct();
21232140
$this->emitsSignal = $emitsSignal;
2141+
$this->signal = $signal;
21242142
}
21252143

21262144
protected function execute(InputInterface $input, OutputInterface $output): int
21272145
{
21282146
if ($this->emitsSignal) {
2129-
posix_kill(posix_getpid(), \SIGUSR1);
2147+
posix_kill(posix_getpid(), $this->signal);
21302148
}
21312149

21322150
for ($i = 0; $i < $this->loop; ++$i) {
21332151
usleep(100);
21342152
if ($this->signaled) {
2135-
return 1;
2153+
return $this->exitCode;
21362154
}
21372155
}
21382156

@@ -2155,6 +2173,22 @@ public function handleSignal(int $signal): void
21552173
}
21562174
}
21572175

2176+
class TerminatableCommand extends BaseSignableCommand implements SignalableCommandInterface
2177+
{
2178+
protected static $defaultName = 'signal';
2179+
2180+
public function getSubscribedSignals(): array
2181+
{
2182+
return SignalRegistry::isSupported() ? [\SIGINT] : [];
2183+
}
2184+
2185+
public function handleSignal(int $signal): void
2186+
{
2187+
$this->signaled = true;
2188+
$this->signalHandlers[] = __CLASS__;
2189+
}
2190+
}
2191+
21582192
class SignalEventSubscriber implements EventSubscriberInterface
21592193
{
21602194
public $signaled = false;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.