Skip to content
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

Ask for bringing back the std::rt::unwind::try #28726

Closed
zonyitoo opened this issue Sep 29, 2015 · 6 comments
Closed

Ask for bringing back the std::rt::unwind::try #28726

zonyitoo opened this issue Sep 29, 2015 · 6 comments

Comments

@zonyitoo
Copy link

Since Rust does not have officially support coroutines, so I attempted to made several crates that supports coroutine scheduling and non-blocking I/O.

But these crates requires std::rt::unwind::try to catch panics inside coroutines to prevent worker thread to panicking and exiting. But since the latest nightly version of Rust have made std::rt to be private, there won't be another way to catch panics ...

So I am here to ask for suggestions:

  1. Could you bring back the std::rt again?
  2. Before the language supported coroutine (the yield keyword) is done, is it possible to let coio-rs to go into the std crate? Just like the libgreen before 0.12-dev. :)

By the way, coroutine-rs also needs the std::rt::unwind::begin_unwind to explicitly start unwinding for preventing memory leaks.

@bluss
Copy link
Member

bluss commented Sep 29, 2015

Check out std::thread::catch_panic (and soon to be named std::panic::recover).

Maybe you want to participate in the discussion around it too, it's ongoing: rust-lang/rfcs/pull/1236

@zonyitoo
Copy link
Author

I don't think std::thread::catch_panic would work in this circumstance, because coroutines may be scheduled between threads, if I use catch_panic to register a callback in one thread's local storage, it won't work!

@bluss
Copy link
Member

bluss commented Sep 29, 2015

catch_panic is just a wrapper for the function you are asking for -- see source. That said, I have no idea about the details. Bring your advanced questions to the catch_panic rfc discussion!

@steveklabnik
Copy link
Member

Yes, please comment on that RFC! And if there's some reason it doesn't work, adding yet another catching mechanism, like that one, would need an RFC anyway, so I'm going to give this issue a close. Thank you!

@alexcrichton
Copy link
Member

As @bluss mentioned, catch_panic is probably what you want for now as it's just a direct wrapper around try. The main difference is that catch_panic has the bounds of Send + 'static on the closure, which is a topic of hot debate over on the RFC!

If you'd like to get the same functionality as before, you can build the old try on top of catch_panic:

#![feature(catch_panic)]

use std::thread;

pub unsafe fn try<R, F: FnOnce() -> R>(f: F) -> thread::Result<R> {
    let mut f = Some(f);
    let f = &mut f as *mut Option<F> as usize;
    thread::catch_panic(move || {
        (*(f as *mut Option<F>)).take().unwrap()()
    })
}

@zonyitoo
Copy link
Author

@alexcrichton Thanks Alex!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants