Skip to content

Commit 47f3ec2

Browse files
committed
docs: document SignalTrait support for Ctrl+C on Windows
1 parent 14de6fa commit 47f3ec2

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

‎user_guide_src/source/changelogs/v4.8.0.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ Commands
270270
- Likewise, the ``command()`` function now also supports the above enhancements for command-line option parsing when using the function to run commands from code.
271271
- Added ``make:request`` generator command to scaffold :ref:`Form Request <form-requests>` classes.
272272
- Added ``key:rotate`` command to demote the current ``encryption.key`` to ``encryption.previousKeys`` in **.env** and generate a new key. See :ref:`spark-key-rotate`.
273+
- ``SignalTrait`` now handles Ctrl+C and Ctrl+Break on Windows, through ``sapi_windows_set_ctrl_handler()``, so a command can stop gracefully there too. The events are reported as ``SIGINT`` (``2``). See :ref:`cli-signals-windows`.
273274
- Added ``AbstractCommand::callSilently()`` to invoke another command with its output discarded, restoring the prior output afterwards. See :ref:`modern-commands-call-silently`.
274275
- The output side of the ``CLI`` class now lives in instance classes, with ``CLI`` delegating to them: :php:class:`Output <CodeIgniter\\CLI\\Output>`
275276
(``print()``, ``write()``, ``error()``, ``newLine()``, ``deferNewLine()``, ``beep()``, ``clearScreen()``, ``color()``, ``wrap()``),

‎user_guide_src/source/cli/cli_signals.rst‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,32 @@ System Requirements
5151

5252
Signal handling requires:
5353

54-
* **Unix-based system** (Linux, macOS, BSD) - Windows is not supported
54+
* **Unix-based system** (Linux, macOS, BSD) - see :ref:`cli-signals-windows` for Windows
5555
* **PCNTL extension** - for signal registration and handling
5656
* **POSIX extension** - required for pause/resume functionality (SIGTSTP/SIGCONT)
5757

5858
.. note:: On systems without these extensions, the SignalTrait will gracefully degrade and disable signal handling.
5959

60+
.. _cli-signals-windows:
61+
62+
Windows
63+
-------
64+
65+
Windows has no PCNTL extension and no Unix signals. Since v4.8.0, ``SignalTrait`` handles the one thing Windows can
66+
deliver to a PHP script: the console control events produced by **Ctrl+C** and **Ctrl+Break**. They are handled through
67+
PHP's ``sapi_windows_set_ctrl_handler()`` and reported to your command as ``SIGINT`` (``2``), so ``isRunning()``,
68+
``shouldTerminate()``, custom handlers, and ``onInterruption()`` work as they do on Unix.
69+
70+
* Call ``registerSignals()`` without arguments. The ``SIG*`` constants come from the PCNTL extension, which is not available on Windows.
71+
If you pass a list, it must contain ``2``. Every other signal is ignored.
72+
* To map the event to your own method, use ``2`` as the key: ``$this->registerSignals([], [2 => 'handleInterrupt']);``.
73+
* ``withSignalsBlocked()`` runs the operation, but cannot block anything. The event does not stop the process by itself once a handler is
74+
registered, so a critical section is still completed. Your handler runs when the event arrives.
75+
* Pause and resume (``SIGTSTP`` / ``SIGCONT``), and every other signal, do not exist on Windows.
76+
77+
.. important:: Once a handler is registered, Ctrl+C and Ctrl+Break no longer end the process. Your command must check
78+
``shouldTerminate()`` and return, as on Unix.
79+
6080
*********************
6181
Using the SignalTrait
6282
*********************
@@ -235,7 +255,7 @@ Class Reference
235255

236256
.. literalinclude:: cli_signals/007.php
237257

238-
.. note:: Requires the PCNTL extension. On Windows, signal handling is automatically disabled.
258+
.. note:: Requires the PCNTL extension. On Windows, only Ctrl+C and Ctrl+Break are handled, as ``SIGINT`` (``2``). See :ref:`cli-signals-windows`.
239259

240260
.. php:method:: isRunning()
241261

0 commit comments

Comments
 (0)