Description
Symfony version(s) affected: 5.3.0
Description
My command was using the SignalableCommandInterface
on Symfony 5.2.x and it was working perfectly, but, after upgrading to Symfony 5.3.0 the command signals didn't get registered when the framework initializes the commands.
After investigation, I found that when Symfony started to call the commands lazily in Symfony 5.3.0, the checking of the command is inheriting SignalableCommandInterface
becomes meaningless (console/Application.php) as the command class becomes Symfony\Component\Console\Command\LazyCommand
instead of my own command class and it can't check if I'm inheriting the SignalableCommandInterface
or not.
How to reproduce
-
Create a new command using the maker bundle and implement the
SignalableCommandInterface
as the following<?php namespace App\Command; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\SignalableCommandInterface; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class TestCommand extends Command implements SignalableCommandInterface { protected static $defaultName = 'app:test-command'; protected static $defaultDescription = 'Add a short description for your command'; protected function execute(InputInterface $input, OutputInterface $output): int { $output->writeln('Starting the process...'); sleep(30); return Command::SUCCESS; } public function getSubscribedSignals(): array { return [SIGINT, SIGTERM]; } public function handleSignal(int $signal): void { var_dump($signal); } }
-
Run the command
bin/console app:test-command
and after it starts press ctrl+c to exit
Possible Solution
The workaround to make signals work again is by disabling the lazy load of the command by removing the var $defaultName
from the command class and bypass the command name in the configure
method as the following:
class TestCommand extends Command implements SignalableCommandInterface
{
// protected static $defaultName = 'app:test-command';
protected static $defaultDescription = 'Add a short description for your command';
protected function configure(): void
{
$this->setName('app:test-command');
}