A missing panic message with tokio and futures::select

Here is a very simple program that panics and displays a message:

use futures::{future::FutureExt, select};

#[tokio::main]
async fn main() {
    select! {
        _ = panic_message().fuse() => {},
    }
}

async fn panic_message() {
    tokio::spawn(async {
        panic!("This message is not displayed!");
    });
}

It runs as expected in playground, but when it is run on my machine, cargo run simply exits without any panic message displayed in the terminal.

Only minimum dependencies are included in Cargo.toml:

futures = "0.3.2"
tokio = { version = "0.2.11", features = ["macros", "rt-core"] }

Not sure if it matters but my machine is an Arch Linux running Rust 1.41.

Can anyone please explain why the panic message can be missing? Thanks!

Rust programs always exit when main returns, so the reason is likely that your spawned task didn't get around to running before you exited main. Try inserting an .await on your tokio::spawn to wait for your spawned task:

use futures::{future::FutureExt, select};

#[tokio::main]
async fn main() {
    select! {
        _ = panic_message().fuse() => {},
    }
}

async fn panic_message() {
    tokio::spawn(async {
        panic!("This message is not displayed!");
    }).await.unwrap();
}

Notice that the unwrap() propogates the panic to the main thread.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.