Skip to content

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

Closed
nagisa opened this issue Apr 5, 2015 · 9 comments
Closed

thread::spawn should not print panic message #24099

nagisa opened this issue Apr 5, 2015 · 9 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@nagisa
Copy link
Member

nagisa commented Apr 5, 2015

http://is.gd/fUQtJN prints a thread '<unnamed>' panicked at 'explicit panic', <anon>:5 even though I supposedly explicitly handle it.

@shepmaster
Copy link
Member

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());
}

@ebfull
Copy link
Contributor

ebfull commented Apr 6, 2015

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)]

@nagisa
Copy link
Member Author

nagisa commented Apr 6, 2015

Why is the std::io::set_panic() function not documented in the
nightly docs?

Because it is an implementation detail of the standard library.

On Mon, 6 Apr, 2015 at 6:50 AM, ebfull [email protected] wrote:

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>>);
impl Write for Sink {
fn write(&mut self, data: &[u8]) -> io::Result {
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?


Reply to this email directly or view it on GitHub.

@alexcrichton
Copy link
Member

cc @aturon and rust-lang/rfcs#946

@steveklabnik
Copy link
Member

With rust-lang/rfcs#1328 merged, this should be helpful for this, yeah?

@aturon
Copy link
Member

aturon commented Dec 18, 2015

@steveklabnik Yes, to some degree. Note, though, that the panic handler will run immediately upon panicking, regardless of any subsequent recovers.

@bltavares
Copy link
Contributor

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());
}

@Mark-Simulacrum
Copy link
Member

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., panic::set_hook). Perhaps the right thing to do is to close, then? If people want this behavior (no printing), they should use a custom hook.

@Mark-Simulacrum
Copy link
Member

panic::set_hook allows disabling or modifying the print message; as such, I'm going to close. For example, code here (#24099 (comment)) now prints (note: doesn't print 'should not print').

thread '<unnamed>' panicked at 'This should print', test.rs:7
note: Run with `RUST_BACKTRACE=1` for a backtrace.
r1: true
r2: true
thread '<unnamed>' panicked at 'This should print', test.rs:19
r3: true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

8 participants