Skip to content

[Console]  Fix signal handlers called after event listeners and skip exit #48210

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
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
8 changes: 4 additions & 4 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -1012,10 +1012,6 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
});
}
}

foreach ($commandSignals as $signal) {
$this->signalRegistry->register($signal, [$command, 'handleSignal']);
}
}

if (null !== $this->dispatcher) {
Expand All @@ -1034,6 +1030,10 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
});
}
}

foreach ($commandSignals as $signal) {
$this->signalRegistry->register($signal, [$command, 'handleSignal']);
}
}

if (null === $this->dispatcher) {
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1975,6 +1975,21 @@ public function testSignalableCommandInterfaceWithoutSignals()
$this->assertSame(0, $application->run(new ArrayInput(['signal'])));
}

public function testSignalableCommandHandlerCalledAfterEventListener()
{
$command = new SignableCommand();

$subscriber = new SignalEventSubscriber();

$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber($subscriber);

$application = $this->createSignalableApplication($command, $dispatcher);
$application->setSignalsToDispatchEvent(\SIGUSR1);
$this->assertSame(1, $application->run(new ArrayInput(['signal'])));
$this->assertSame([SignalEventSubscriber::class, SignableCommand::class], $command->signalHandlers);
}

/**
* @group tty
*/
Expand Down Expand Up @@ -2076,6 +2091,7 @@ public function isEnabled(): bool
class BaseSignableCommand extends Command
{
public $signaled = false;
public $signalHandlers = [];
public $loop = 1000;
private $emitsSignal;

Expand Down Expand Up @@ -2116,6 +2132,7 @@ public function getSubscribedSignals(): array
public function handleSignal(int $signal): void
{
$this->signaled = true;
$this->signalHandlers[] = __CLASS__;
}
}

Expand All @@ -2127,6 +2144,7 @@ public function onSignal(ConsoleSignalEvent $event): void
{
$this->signaled = true;
$event->getCommand()->signaled = true;
$event->getCommand()->signalHandlers[] = __CLASS__;
}

public static function getSubscribedEvents(): array
Expand Down