codebase.watch what is new in your dependencies

Exit code of a signal: 0 → 128 + $signal

At a glance

Type
BC break
Exit code 128 + $signal added
8.2

Since symfony/console 8.2, an application stopped by a signal exits with 128 + the signal number instead of 0.

It is the exit code a shell reports for a process killed by this signal: 130 for a Ctrl+C (SIGINT), 143 for SIGTERM.

BC break

The application handles the signals when it has an event dispatcher (always in a Symfony application), or when the command implements SignalableCommandInterface.

The command sends itself a SIGINT, the signal of a Ctrl+C.

Signal stopping the application

Since 5.2, the application dispatching the signals exited with 0.

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\EventDispatcher\EventDispatcher;

$application = new Application();
$application->setDispatcher(new EventDispatcher());
$application
    ->register('foo')
    ->setCode(function (): int {
        posix_kill(posix_getpid(), SIGINT);

        return Command::SUCCESS;
    });

// 5.2 – 8.1: Exit code: 0
// >= 8.2: Exit code: 130
$application->run(new ArrayInput(['foo']));

Exit code read by a listener

Since 6.3, a ConsoleEvents::SIGNAL listener reads the exit code with getExitCode(): it starts with 128 + the signal number.

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleSignalEvent;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\EventDispatcher\EventDispatcher;

$dispatcher = new EventDispatcher();
$dispatcher->addListener(
    ConsoleEvents::SIGNAL,
    function (ConsoleSignalEvent $event): void {
        // 6.3 – 8.1: Exit code: 0
        // >= 8.2: Exit code: 130
        $exitCode = $event->getExitCode();
    }
);

$application = new Application();
$application->setDispatcher($dispatcher);

$application
    ->register('foo')
    ->setCode(function (): int {
        posix_kill(posix_getpid(), SIGINT);

        return Command::SUCCESS;
    });

// 6.3 – 8.1: Exit code: 0
// >= 8.2: Exit code: 130
$application->run(new ArrayInput(['foo']));

Exit code received by a signalable command

A command implementing SignalableCommandInterface receives the exit code in $previousExitCode, even without an event dispatcher.

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\SignalableCommandInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

$application = new Application();
$application->addCommand(new class('foo') extends Command implements SignalableCommandInterface {
    public function getSubscribedSignals(): array
    {
        return [SIGINT];
    }

    public function handleSignal(int $signal, int|false $previousExitCode = 0): int|false
    {
        // 7.4 – 8.1: Exit code: 0
        // >= 8.2: Exit code: 130
        return $previousExitCode;
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        posix_kill(posix_getpid(), SIGINT);

        return Command::SUCCESS;
    }
});

// 7.4 – 8.1: Exit code: 0
// >= 8.2: Exit code: 130
$application->run(new ArrayInput(['foo']));

Migration

symfony/console 8.2 throws no error: only the exit code changes.

What reads the exit code of a command stopped by a signal now sees a failure: a script, a CI job, a process manager.

A command run as a systemd service that does not handle SIGTERM ends up failed after systemctl stop, with the exit code 143.

Keep exiting with 0

A ConsoleEvents::SIGNAL listener sets the exit code back to 0 with setExitCode().

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleSignalEvent;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\EventDispatcher\EventDispatcher;

$dispatcher = new EventDispatcher();
$dispatcher->addListener(
    ConsoleEvents::SIGNAL,
    function (ConsoleSignalEvent $event): void {
        $event->setExitCode(0);
    }
);

$application = new Application();
$application->setDispatcher($dispatcher);

$application
    ->register('foo')
    ->setCode(function (): int {
        posix_kill(posix_getpid(), SIGTERM);

        return Command::SUCCESS;
    });

// Exit code: 0
$application->run(new ArrayInput(['foo']));

For a systemd service, adding SuccessExitStatus=143 to its unit also keeps it from ending up failed.

To find the code to migrate, search the listeners of ConsoleEvents::SIGNAL, the handleSignal() methods returning $previousExitCode, and what reads the exit code of your commands.

The sources