-
Notifications
You must be signed in to change notification settings - Fork 13.3k
thread::spawn should not print panic message #24099
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The code in question, in case the link goes stale: use std::thread;
fn main() {
let r = thread::spawn(|| {
panic!();
}).join();
println!("{:?}", r.is_err());
} |
This is one of the things which is not stable yet. This is how rustc does it: #![feature(set_stdio)]
use std::thread;
use std::io;
use std::io::prelude::*;
use std::sync::{Arc,Mutex};
fn main() {
struct Sink(Arc<Mutex<Vec<u8>>>);
impl Write for Sink {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
Write::write(&mut *self.0.lock().unwrap(), data)
}
fn flush(&mut self) -> io::Result<()> { Ok(()) }
}
let data = Arc::new(Mutex::new(Vec::new()));
let err = Sink(data.clone());
let r = thread::spawn(|| {
io::set_panic(Box::new(err));
panic!();
}).join();
println!("{:?}", r.is_err());
} Why is the std::io::set_panic() function not documented in the nightly docs? edit: nvm it's #[doc(hidden)] |
Because it is an implementation detail of the standard library. On Mon, 6 Apr, 2015 at 6:50 AM, ebfull [email protected] wrote:
|
cc @aturon and rust-lang/rfcs#946 |
With rust-lang/rfcs#1328 merged, this should be helpful for this, yeah? |
@steveklabnik Yes, to some degree. Note, though, that the panic handler will run immediately upon panicking, regardless of any subsequent recovers. |
Triaging: given that we have the rust-lang/rfcs#1328 merged, and stabilised already, should we update the code example to the following? #![feature(panic_handler)]
use std::panic;
use std::thread;
fn main() {
let r1 = thread::spawn(|| {
panic!("This should print");
}).join();
println!("r1: {:?}", r1.is_err());
let r2 = thread::spawn(|| {
panic::set_hook(Box::new(|_| ()));
panic!("This should not print");
}).join();
panic::take_hook();
println!("r2: {:?}", r2.is_err());
let r3 = thread::spawn(|| {
panic!("This should print");
}).join();
println!("r3: {:?}", r3.is_err());
} |
What is intended to happen here? As far as I know, the print on panic behavior isn't really possible to change, though we do have features that are either stabilized or on track that solve this (e.g., |
|
http://is.gd/fUQtJN prints a
thread '<unnamed>' panicked at 'explicit panic', <anon>:5
even though I supposedly explicitly handle it.The text was updated successfully, but these errors were encountered: