-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
UnwindSafe docs are unclear #65717
Comments
I haven't actually looked at these docs, so I can't agree or disagree :) Thanks for opening the issue. I wish that there was something more... actionable, about it, but this is a good first step 👍 |
Yes, me too. I'm not sure how to word what I got from reading these docs. I think my first intention is to clarify whether "it is just me" and I should re-read the docs again until it makes sense or whether other people have trouble understanding these. The docs aren't short, there are a lot of words in there, and a lot of things are explained. By "unclear" I I think I more precisely mean that, after reading the docs, I cannot answer basic questions about what I think the docs attempted to explain:
There is also the issue that one needs to click through a lot of pieces to obtain the whole picture (e.g. the UnwindSafe trait docs, the AssertUnwindSafe docs, the RFC, the nomicon, etc.). I think it might be better to cover panic safety holistically in the Another reason why the documentation feels unclear is because there is actually a lot of content in there, but reading it doesn't feel "definitive". E.g. it feels as if it isn't clear at all what "panic safety" is, nor what problems do these utilities solve, if any, and / or whether they are good solutions to these problems. I'm not sure how else to explain it, but I run into this docs once a year, and I've never felt I truly understood what the point of these features are. #40628 mentions
and... this is what I ended up doing last time, and what I ended up doing today. After reading the docs, I'm not even able to write a comment saying whether doing that is "ok". In January or so I remember running into this again here: Line 570 in d6e4028
AssertUnwindSafe is correct there. I tried to write a comment, but if you take a look the function that gets passed to catch_unwind is a generic closure, so... it can be anything. How can one know that it is definitely going to be unwind safe ?
|
If
then it must be correct, right? |
@jakoschiko I think all of those libraries use lazy_static! { static ref V: Mutex<Vec<u8>> = Mutex::new(vec![]); }
#[should_panic] #[test] fn foo() { unsafe {
{ let mut v = V.lock().unwrap(); v.reserve(42); v.set_len(42); }
panic!();
}}
#[test] fn bar() {
let v = V.lock().unwrap(); let v: &Vec<u8> = &*v;
for i in v { dbg!(*i); }
} Here if The whole point of
or something like that. They could then offer a way for the test writer to specify that their test is #[assert_unwind_safe]
#[should_panic] #[test] fn foo() { unsafe {
{ let mut v = V.lock().unwrap(); v.reserve(42); v.set_len(42); }
panic!();
}} or similar. For example, they could also require that functions with |
UnwindSafe is not a "real" safety barrier- |
@sfackler What you mention is covered in the These libraries don't know what that code does, so why are they asserting that such code is
True, and
That's a different problem. Let's assume a single threaded runtime like the ones quickcheck, proptest, or libtest with RUST_TEST_THREADS=1 use. |
|
@sfackler if that's the case, then the uses in libstd (and by the people in #40628 that just stamp
which is a very different thing from expressing that one does not care about unwind safety. The wording that you propose makes more sense to me. |
rust-lang/rust#40628, rust-lang/rust#65717 and rust-lang/rfcs#3260 all show that unwind safety isn't particularly ergonomic to use and implement, and ultimately leads to people slapping `AssertUnwindSafe` everywhere until the compiler stops complaining. This situation has led to built-in test framework using `catch_unwind(AssertUnwindSafe(...))` (see https://github.com/rust-lang/rust/blob/1.73.0/library/test/src/lib.rs#L649) and libraries like tower-http doing the same (see https://docs.rs/tower-http/0.4.4/src/tower_http/catch_panic.rs.html#198). As people have mentioned in the threads above, trying to implement this correctly is akin to fighting windmills at the moment. Since the above cases demonstrated that `catch_unwind(AssertUnwindSafe(...))` is currently the easiest way to deal with this situation, this commit does the same and refactors our background job runner code accordingly.
rust-lang/rust#40628, rust-lang/rust#65717 and rust-lang/rfcs#3260 all show that unwind safety isn't particularly ergonomic to use and implement, and ultimately leads to people slapping `AssertUnwindSafe` everywhere until the compiler stops complaining. This situation has led to built-in test framework using `catch_unwind(AssertUnwindSafe(...))` (see https://github.com/rust-lang/rust/blob/1.73.0/library/test/src/lib.rs#L649) and libraries like tower-http doing the same (see https://docs.rs/tower-http/0.4.4/src/tower_http/catch_panic.rs.html#198). As people have mentioned in the threads above, trying to implement this correctly is akin to fighting windmills at the moment. Since the above cases demonstrated that `catch_unwind(AssertUnwindSafe(...))` is currently the easiest way to deal with this situation, this commit does the same and refactors our background job runner code accordingly.
After reading the current docs of
panic::UnwindSafe
I am left with the feeling that the explanation of the concept of "panic safety" and its auto trait has only been handwaved. The docs point to the RFC, which does a better job, but I still do not feel like I fully understood what "panic safety" is, what doesUnwindSafe
convey, and when is it ok to useAssertUnwindSafe
or not.I feel like the docs could do a much better job at explaining all of this, and that some examples in the RFC would help, but I don't think that any documentation we currently have about this is enough.
This is a complex topic and I have no experience teaching it to others, but if somebody has done that, their feedback should probably be taken into account.
I think the API docs are very important to get right because they are currently the only teaching materials that we have about this aspect of the language. The Rust book does not cover this either, and the nomicon Exception safety has very few words to say about this.
cc @steveklabnik @rust-lang/docs
The text was updated successfully, but these errors were encountered: