Struct Signal

struct Signal { ... }

An listener for receiving a particular type of OS signal.

The listener can be turned into a Stream using SignalStream.

In general signal handling on Unix is a pretty tricky topic, and this structure is no exception! There are some important limitations to keep in mind when using Signal streams:

If you've got any questions about this feel free to open an issue on the repo! New approaches to alleviate some of these limitations are always appreciated!

Caveats

The first time that a Signal instance is registered for a particular signal kind, an OS signal-handler is installed which replaces the default platform behavior when that signal is received, for the duration of the entire process.

For example, Unix systems will terminate a process by default when it receives SIGINT. But, when a Signal instance is created to listen for this signal, the next SIGINT that arrives will be translated to a stream event, and the process will continue to execute. Even if this Signal instance is dropped, subsequent SIGINT deliveries will end up captured by Tokio, and the default platform behavior will NOT be reset.

Thus, applications should take care to ensure the expected signal behavior occurs as expected after listening for specific signals.

Examples

Wait for SIGHUP

use tokio::signal::unix::{signal, SignalKind};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // An infinite stream of hangup signals.
    let mut sig = signal(SignalKind::hangup())?;

    // Print whenever a HUP signal is received
    loop {
        sig.recv().await;
        println!("got signal HUP");
    }
}

Implementations

impl Signal

async fn recv(self: &mut Self) -> Option<()>

Receives the next signal notification event.

None is returned if no more events can be received by this stream.

Cancel safety

This method is cancel safe. If you use it as the event in a tokio::select! statement and some other branch completes first, then it is guaranteed that no signal is lost.

Examples

Wait for SIGHUP

use tokio::signal::unix::{signal, SignalKind};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // An infinite stream of hangup signals.
    let mut stream = signal(SignalKind::hangup())?;

    // Print whenever a HUP signal is received
    loop {
        stream.recv().await;
        println!("got signal HUP");
    }
}
fn poll_recv(self: &mut Self, cx: &mut Context<'_>) -> Poll<Option<()>>

Polls to receive the next signal notification event, outside of an async context.

This method returns:

  • Poll::Pending if no signals are available but the channel is not closed.
  • Poll::Ready(Some(())) if a signal is available.
  • Poll::Ready(None) if the channel has been closed and all signals sent before it was closed have been received.

Examples

Polling from a manually implemented future

use std::pin::Pin;
use std::future::Future;
use std::task::{Context, Poll};
use tokio::signal::unix::Signal;

struct MyFuture {
    signal: Signal,
}

impl Future for MyFuture {
    type Output = Option<()>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        println!("polling MyFuture");
        self.signal.poll_recv(cx)
    }
}

impl Debug for Signal

fn fmt(self: &Self, f: &mut $crate::fmt::Formatter<'_>) -> $crate::fmt::Result

impl Freeze for Signal

impl RefUnwindSafe for Signal

impl Send for Signal

impl Sync for Signal

impl Unpin for Signal

impl UnwindSafe for Signal

impl<T> Any for Signal

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Signal

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Signal

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> From for Signal

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into for Signal

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for Signal

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for Signal

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>