Skip to content

Command signals doesn't get registered if the command is lazy loaded #41583

Closed
@khal3d

Description

@khal3d

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

  1. 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);
        }
    }
  2. 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');
    }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions