Skip to content

[Console] Don't register signal handlers if pcntl is disabled #38589

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 1 commit into from
Oct 16, 2020
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: 6 additions & 2 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Symfony\Component\Console\Exception\ExceptionInterface;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Exception\NamespaceNotFoundException;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Helper\DebugFormatterHelper;
use Symfony\Component\Console\Helper\FormatterHelper;
Expand Down Expand Up @@ -88,8 +89,8 @@ public function __construct(string $name = 'UNKNOWN', string $version = 'UNKNOWN
$this->version = $version;
$this->terminal = new Terminal();
$this->defaultCommand = 'list';
$this->signalRegistry = new SignalRegistry();
if (\defined('SIGINT')) {
if (\defined('SIGINT') && SignalRegistry::isSupported()) {
$this->signalRegistry = new SignalRegistry();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late review, but the PR has changed before my review.
So here we go:

This change is wrong. See getSignalRegistry() method:
https://github.com/chalasr/symfony/blob/8fe876341e860ea3fa13bec3c2bdf77a66ead424/src/Symfony/Component/Console/Application.php#L111-L114

Then, a lot of fatal error could occurs since the registry might not be defined

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, thank you; #38602 makes it nullable

$this->signalsToDispatchEvent = [\SIGINT, \SIGTERM, \SIGUSR1, \SIGUSR2];
}
}
Expand Down Expand Up @@ -953,6 +954,9 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
}

if ($command instanceof SignalableCommandInterface) {
if (!$this->signalsToDispatchEvent) {
throw new RuntimeException('Unable to subscribe to signal events. Make sure that the `pcntl` extension is installed and that "pcntl_*" functions are not disabled by your php.ini\'s "disable_functions" directive.');
}
foreach ($command->getSubscribedSignals() as $signal) {
$this->signalRegistry->register($signal, [$command, 'handleSignal']);
}
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/Console/SignalRegistry/SignalRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ public function register(int $signal, callable $signalHandler): void
pcntl_signal($signal, [$this, 'handle']);
}

public static function isSupported(): bool
{
if (!\function_exists('pcntl_signal')) {
return false;
}

if (\in_array('pcntl_signal', explode(',', ini_get('disable_functions')))) {
return false;
}

return true;
}

/**
* @internal
*/
Expand Down