Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Do not panic while panicking (#7167)
Browse files Browse the repository at this point in the history
* Do not panic while panicking

* Update primitives/runtime/src/lib.rs

Co-authored-by: David <[email protected]>

* Move function to `sp-std`

Co-authored-by: David <[email protected]>
  • Loading branch information
bkchr and dvdplm authored Sep 22, 2020
1 parent 458e4a5 commit 07f480b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
19 changes: 18 additions & 1 deletion primitives/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,10 @@ impl SignatureBatching {
impl Drop for SignatureBatching {
fn drop(&mut self) {
// Sanity check. If user forgets to actually call `verify()`.
if !self.0 {
//
// We should not panic if the current thread is already panicking,
// because Rust otherwise aborts the process.
if !self.0 && !sp_std::thread::panicking() {
panic!("Signature verification has not been called before `SignatureBatching::drop`")
}
}
Expand Down Expand Up @@ -885,4 +888,18 @@ mod tests {
);
});
}

#[test]
#[should_panic(expected = "Hey, I'm an error")]
fn batching_does_not_panic_while_thread_is_already_panicking() {
let mut ext = sp_state_machine::BasicExternalities::default();
ext.register_extension(
sp_core::traits::TaskExecutorExt::new(sp_core::testing::TaskExecutor::new()),
);

ext.execute_with(|| {
let _batching = SignatureBatching::start();
panic!("Hey, I'm an error");
});
}
}
4 changes: 4 additions & 0 deletions primitives/std/with_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ pub mod collections {
pub use std::collections::btree_set;
pub use std::collections::vec_deque;
}

pub mod thread {
pub use std::thread::panicking;
}
9 changes: 9 additions & 0 deletions primitives/std/without_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,12 @@ pub mod borrow {
pub use core::borrow::*;
pub use alloc::borrow::*;
}

pub mod thread {
/// Returns if the current thread is panicking.
///
/// In wasm this always returns `false`, as we abort on any panic.
pub fn panicking() -> bool {
false
}
}

0 comments on commit 07f480b

Please sign in to comment.