Skip to content
CryoCryo home
Stdlibprocess

signal_handling

import std::process::signal_handling; · source

Receiving the interrupt and terminate signals, so a program can shut down cleanly instead of being ended. Signals::listen() starts receiving them. While any Signals is alive they no longer end the process: wait, wait_timeout, and try_wait hand each one over instead. Once the last Signals is dropped they end the process again, as they did before.

import std::process::signal::{ Signal };
import std::process::signal_handling::{ Signals };

const signals: Signals = Signals::listen()?;
const received: Signal = signals.wait();   // Ctrl-C, or a SIGTERM

What arrives:

PlatformInterrupt (Signal::interrupt())Terminate (Signal::terminate())
POSIXSIGINTSIGTERM
WindowsCtrl-C and Ctrl-Breakclosing the console, logging off, shutting down

A terminate on Windows cannot be refused: the process ends once the program exits or Windows' time limit runs out, whichever comes first. The limit is a system setting, by default 5 seconds (20 for a service at shutdown), so cleanup after it must be quick. Until then the program runs as normal and wait hands the terminate over. Logoff and shutdown reach only services, and only a program that has not loaded the Windows user interface libraries (user32 or gdi32); Windows ends any other program at logoff and shutdown without a signal.

Every Signals in the process shares one record of what arrived, so each signal is handed to one waiter, whichever asks first. Signals that arrive before anyone asks are each kept; when both kinds are waiting, an interrupt is handed over before a terminate.

Types

Signals

type struct Signals {}

listen() fails only when the operating system refuses to set up the handler or the channel it wakes waiters through. wait() blocks until a signal is handed over, wait_timeout(duration) gives up with None when the time is up, and try_wait() answers None at once when nothing is waiting.

Constructors

static listen() -> Result<Signals, IoError>;

Start receiving the interrupt and terminate signals. From now until the last Signals is dropped they no longer end the process. An error is the operating system's refusal to set up the handler or the channel it wakes waiters through.

Methods

try_wait(&this) -> Option<Signal>;

The next signal not yet handed over, or None at once when there is none.

wait(&this) -> Signal;

Block until a signal arrives, and hand it over. One that arrived before the call is handed over at once.

wait_timeout(&this, duration: Duration) -> Option<Signal>;

As wait, giving up with None once duration has passed.

Trait implementations

implement trait Drop for struct Signals