tokio::signal module provides asynchronous signal handling for both Unix and Windows platforms.
Overview
This module allows you to listen for OS signals in an asynchronous manner, integrating signal handling into Tokio’s async runtime without blocking.Cross-Platform: Ctrl-C
Thectrl_c function provides a portable way to receive CTRL+C notifications on both Unix and Windows.
Completes when a “ctrl-c” notification is sent to the process.Returns:
io::Result<()>The future completes on the first CTRL+C received after polling begins.Background Listening
You can spawn a task to listen for CTRL+C in the background:Graceful Shutdown Example
Unix Signals
Unix signal handling is only available on Unix platforms with the
signal feature enabled.unix submodule provides Unix-specific signal handling:
SignalKind
Represents specific Unix signals:Represents
SIGALRM - real-time timer expired.Default behavior: Process is terminatedRepresents
SIGCHLD - child process status changed.Default behavior: Signal is ignoredRepresents
SIGHUP - terminal disconnected.Default behavior: Process is terminatedRepresents
SIGINT - interrupt signal (Ctrl-C).Default behavior: Process is terminatedRepresents
SIGIO - I/O operations possible on file descriptor.Default behavior: Signal is ignoredRepresents
SIGPIPE - write to pipe with no reader.Default behavior: Process is terminatedRepresents
SIGQUIT - quit signal with core dump.Default behavior: Process is terminated with core dumpRepresents
SIGTERM - termination signal.Default behavior: Process is terminatedRepresents
SIGUSR1 - user-defined signal 1.Default behavior: Process is terminatedRepresents
SIGUSR2 - user-defined signal 2.Default behavior: Process is terminatedRepresents
SIGWINCH - terminal window size changed.Default behavior: Signal is ignoredCustom Signal Numbers
Creates a SignalKind from a raw signal number.Parameters:
signum: c_int- Raw signal number
SignalKindUseful for platform-specific signals:Gets the signal’s numeric value.Returns:
c_intSignal Stream
Thesignal function returns a stream that yields each time the signal is received:
Methods
Waits for the next signal notification.Returns:
Option<()>Returns None if the signal stream is closed.Multiple Signal Handling
You can listen to multiple signals simultaneously usingtokio::select!:
Important Caveats
Signal Handler Replacement
For example, on Unix, processes normally terminate when receiving SIGINT (Ctrl-C). Once you create a signal listener:Proper Signal Handling
Complete Example
Here’s a complete example showing graceful shutdown with multiple signals:Platform-Specific Notes
Unix
- Signal handlers are process-wide and persist for the process lifetime
- Real-time signals are supported on Linux and illumos
- Some signals cannot be caught (e.g., SIGKILL, SIGSTOP)
Windows
Windows has limited signal support. Thectrl_c function is the primary cross-platform option.
See Also
- tokio::select! - For handling multiple signals
- signal(7) - Linux signal overview
- Process signals - For handling child process signals