Skip to content

Commit

Permalink
Fix signal handlers called after event listeners and skip exit
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Nov 13, 2022
1 parent 005ed05 commit 01c90db
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -1012,10 +1012,6 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
});
}
}

foreach ($commandSignals as $signal) {
$this->signalRegistry->register($signal, [$command, 'handleSignal']);
}
}

if (null !== $this->dispatcher) {
Expand All @@ -1034,6 +1030,10 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
});
}
}

foreach ($commandSignals as $signal) {
$this->signalRegistry->register($signal, [$command, 'handleSignal']);
}
}

if (null === $this->dispatcher) {
Expand Down
18 changes: 18 additions & 0 deletions Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1975,6 +1975,21 @@ public function testSignalableCommandInterfaceWithoutSignals()
$this->assertSame(0, $application->run(new ArrayInput(['signal'])));
}

public function testSignalableCommandHandlerCalledAfterEventListener()
{
$command = new SignableCommand();

$subscriber = new SignalEventSubscriber();

$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber($subscriber);

$application = $this->createSignalableApplication($command, $dispatcher);
$application->setSignalsToDispatchEvent(\SIGUSR1);
$this->assertSame(1, $application->run(new ArrayInput(['signal'])));
$this->assertSame([SignalEventSubscriber::class, SignableCommand::class], $command->signalHandlers);
}

/**
* @group tty
*/
Expand Down Expand Up @@ -2076,6 +2091,7 @@ public function isEnabled(): bool
class BaseSignableCommand extends Command
{
public $signaled = false;
public $signalHandlers = [];
public $loop = 1000;
private $emitsSignal;

Expand Down Expand Up @@ -2116,6 +2132,7 @@ public function getSubscribedSignals(): array
public function handleSignal(int $signal): void
{
$this->signaled = true;
$this->signalHandlers[] = __CLASS__;
}
}

Expand All @@ -2127,6 +2144,7 @@ public function onSignal(ConsoleSignalEvent $event): void
{
$this->signaled = true;
$event->getCommand()->signaled = true;
$event->getCommand()->signalHandlers[] = __CLASS__;
}

public static function getSubscribedEvents(): array
Expand Down

0 comments on commit 01c90db

Please sign in to comment.