Skip to content

feat: handle Ctrl+C and Ctrl+Break in SignalTrait on Windows - #10699

Open
mdalikadar wants to merge 4 commits into
codeigniter4:4.8from
mdalikadar:feat/signaltrait-windows-ctrl-c
Open

mdalikadar wants to merge 4 commits into
codeigniter4:4.8from
mdalikadar:feat/signaltrait-windows-ctrl-c

Conversation

@mdalikadar

Copy link
Copy Markdown
Contributor

Description
Adds Windows support to SignalTrait, as discussed in #10589. Without PCNTL, registerSignals() now installs a console control handler through sapi_windows_set_ctrl_handler(). It is reported to the command as SIGINT (2), because the SIG* constants come from PCNTL and are not available on Windows. A command can then stop gracefully on Ctrl+C or Ctrl+Break through shouldTerminate(), onInterruption() or a mapped method, as it does on Unix.

  • unregisterSignals() removes the handler.
  • getSignalName() no longer touches the SIG* constants without PCNTL, so getProcessState() works there.
  • withSignalsBlocked() stays a no-op on Windows, which is documented.
  • The native function sits behind a small protected wrapper so tests can replace it. The new tests run on any OS.

Refs #10589

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@carson-codeigniter4 carson-codeigniter4 Bot added 4.8 PRs that target the `4.8` branch. stale Pull requests with conflicts labels Nov 1, 2026
@carson-codeigniter4

This comment has been minimized.

@mdalikadar
mdalikadar force-pushed the feat/signaltrait-windows-ctrl-c branch from d93de33 to 965afec Compare November 5, 2026 02:23
@carson-codeigniter4 carson-codeigniter4 Bot removed the stale Pull requests with conflicts label Nov 5, 2026
@paulbalandan
paulbalandan requested a balanced review from Copilot November 5, 2026 13:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Windows signal handling has unresolved callback-access failures, unsupported web-SAPI registration, and resource-retention defects, while the no-backend test can still install a native handler.

4 open issues
What changed in this PR

Adds Windows console-event support to SignalTrait, addressing #10589 by letting CLI commands handle Ctrl+C and Ctrl+Break through the existing graceful-termination APIs.

Changes:

  • Registers and removes Windows console handlers, reporting both events as SIGINT (2).
  • Adds simulated Windows tests for registration, dispatch, cleanup, and operation without PCNTL.
  • Documents Windows usage and limitations in the user guide and v4.8.0 changelog.

Validation used focused source inspection; runtime tests and full validation remain for CI.

File Description
tests/​system/​CLI/​SignalWindowsTest.php Tests Windows signal behavior using simulated handlers.
tests/​_support/​Commands/​Legacy/​SignalCommandWindows.php Provides a Windows handler test double.
system/​CLI/​SignalTrait.php Adds Windows registration, event dispatch, cleanup, and signal naming.
docs/​cli/​cli_signals.md Explains Windows usage and signal-blocking limitations.
docs/​changelogs/​v4.8.0.md Announces Windows console-event support.

Review effort: Balanced

{
// Static analysis on other platforms does not know the function.
/** @psalm-suppress UndefinedFunction */
return sapi_windows_set_ctrl_handler($handler, $add); // @codeCoverageIgnore
{
// Neither PCNTL nor a console control handler: Unix without PCNTL, or Windows
// before sapi_windows_set_ctrl_handler() existed.
$command = new SignalCommandNoPcntl(service('logger'), service('commands'));
*/
protected function isWindowsCtrlHandlerAvailable(): bool
{
return is_windows() && function_exists('sapi_windows_set_ctrl_handler');
protected function unregisterSignals(): void
{
if ($this->windowsCtrlHandlerRegistered) {
$this->setWindowsCtrlHandler([$this, 'handleWindowsCtrlEvent'], false);

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.8 PRs that target the `4.8` branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants