Spawned task panics instead of returning error in JoinHandle #5624
-
Hi all, One of the doc examples isn't behaving how I expect: it panics when I expect it to return a Here is the example code: use tokio::task;
use std::io;
use std::panic;
#[tokio::main]
async fn main() -> io::Result<()> {
let join_handle: task::JoinHandle<Result<i32, io::Error>> = tokio::spawn(async {
panic!("boom");
});
let err = join_handle.await.unwrap_err();
assert!(err.is_panic());
Ok(())
} The docs state that:
However, when I run the example code locally or in the Rust playground, I see the panic crash the entire program. Am I misunderstanding the docs? Perhaps there is some configuration that I am missing? Here is a link to a playground that illustrates this behavior. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The panic message is printed even if you catch it. Try this: use tokio::task;
use std::io;
use std::panic;
#[tokio::main]
async fn main() -> io::Result<()> {
let join_handle: task::JoinHandle<Result<i32, io::Error>> = tokio::spawn(async {
panic!("boom");
});
let err = join_handle.await.unwrap_err();
assert!(err.is_panic());
println!("at the end");
Ok(())
} |
Beta Was this translation helpful? Give feedback.
The panic message is printed even if you catch it. Try this: