Closed Lej77 closed 1 year ago
I like this trick. One question: is the panic on unwind = abort behavior guaranteed? I read about it somewhere but can't remember if it was official documentation or not.
I am not sure actually! That is why I added that nested let _bomb = AbortBomb;
statement, to cause infinite recursion if it somehow doesn't lead to an abort. Still I can't think of any reason they would change that. From what I understand there has been talk about making all panics in Drop implementations lead to abort. So it seems likely that if there was a change it would be to more aggressively abort.
If you want to be safer and you are using std
anyways, then just call the std::process::abort
function inside the AbortBomb
's Drop
implementation.
Edit: "double panic" seems to be mentioned in RFC 1328.
Awesome. Since it's abort or infinite recursion anyway I don't think we need to use std::process::abort
.
Its probably better to use a forgettable zero sized type with a custom
Drop
implementation to abort on panic instead of usingstd::panic::catch_unwind
.I think I first saw this pattern described at dtolnay/case-studies/function-epilogue.
This pattern works on
no_std
and is also more future proof in case Rust ever supports unwinds that can't be caught (maybe fromc++
?) as this pattern should abort on those unwinds as well.