8000 [Console] fix restoring stty mode on CTRL+C by nicolas-grekas · Pull Request #45109 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] fix restoring stty mode on CTRL+C #45109

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

if (Terminal::hasSttyAvailable()) {
$sttyMode = shell_exec('stty -g');

foreach ([\SIGINT, \SIGTERM] as $signal) {
$this->signalRegistry->register($signal, static function () use ($sttyMode) {
shell_exec('stty '.$sttyMode);
});
}
}

if ($this->dispatcher) {
foreach ($this->signalsToDispatchEvent as $signal) {
$event = new ConsoleSignalEvent($command, $input, $output, $signal);
Expand Down
13 changes: 10 additions & 3 deletions src/Symfony/Component/Console/Helper/QuestionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
$numMatches = \count($matches);

$sttyMode = shell_exec('stty -g');
$isStdin = 'php://stdin' === (stream_get_meta_data($inputStream)['uri'] ?? null);
$r = [$inputStream];
$w = [];

// Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead)
shell_exec('stty -icanon -echo');
Expand All @@ -257,11 +260,15 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu

// Read a keypress
while (!feof($inputStream)) {
while ($isStdin && 0 === @stream_select($r, $w, $w, 0, 100)) {
// Give signal handlers a chance to run
$r = [$inputStream];
}
$c = fread($inputStream, 1);

// as opposed to fgets(), fread() returns an empty string when the stream content is empty, not false.
if (false === $c || ('' === $ret && '' === $c && null === $question->getDefault())) {
shell_exec(sprintf('stty %s', $sttyMode));
shell_exec('stty '.$sttyMode);
throw new MissingInputException('Aborted.');
} elseif ("\177" === $c) { // Backspace Character
if (0 === $numMatches && 0 !== $i) {
Expand Down Expand Up @@ -366,7 +373,7 @@ function ($match) use ($ret) {
}

// Reset stty so it behaves normally again
shell_exec(sprintf('stty %s', $sttyMode));
shell_exec('stty '.$sttyMode);

return $fullChoice;
}
Expand Down Expand Up @@ -427,7 +434,7 @@ private function getHiddenResponse(OutputInterface $output, $inputStream, bool $
$value = fgets($inputStream, 4096);

if (self::$stty && Terminal::hasSttyAvailable()) {
shell_exec(sprintf('stty %s', $sttyMode));
shell_exec('stty '.$sttyMode);
}

if (false === $value) {
Expand Down
35 changes: 35 additions & 0 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\Console\SignalRegistry\SignalRegistry;
use Symfony\Component\Console\Terminal;
use Symfony\Component\Console\Tester\ApplicationTester;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Process\Process;

class ApplicationTest extends TestCase
{
Expand Down Expand Up @@ -1882,6 +1884,39 @@ public function testSignalableCommandInterfaceWithoutSignals()
$application->add($command);
$this->assertSame(0, $application->run(new ArrayInput(['signal'])));
}

/**
* @group tty
*/
public function testSignalableRestoresStty()
{
if (!Terminal::hasSttyAvailable()) {
$this->markTestSkipped('stty not available');
}

if (!SignalRegistry::isSupported()) {
$this->markTestSkipped('pcntl signals not available');
}

$previousSttyMode = shell_exec('stty -g');

$p = new Process(['php', __DIR__.'/Fixtures/application_signalable.php']);
$p->setTty(true);
$p->start();

for ($i = 0; $i < 10 && shell_exec('stty -g') === $previousSttyMode; ++$i) {
usleep(100000);
}

$this->assertNotSame($previousSttyMode, shell_exec('stty -g'));
$p->signal(\SIGINT);
$p->wait();

$sttyMode = shell_exec('stty -g');
shell_exec('stty '.$previousSttyMode);

$this->assertSame($previousSttyMode, $sttyMode);
}
}

class CustomApplication extends Application
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\SignalableCommandInterface;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\SingleCommandApplication;

$vendor = __DIR__;
while (!file_exists($vendor.'/vendor')) {
$vendor = \dirname($vendor);
}
require $vendor.'/vendor/autoload.php';

(new class() extends SingleCommandApplication implements SignalableCommandInterface {
public function getSubscribedSignals(): array
{
return [SIGINT];
}

public function handleSignal(int $signal): void
{
exit;
}
})
->setCode(function(InputInterface $input, OutputInterface $output) {
$this->getHelper('question')
->ask($input, $output, new ChoiceQuestion('😊', ['y']));

return 0;
})
->run()

;
0