diff --git a/substrate/primitives/runtime/src/lib.rs b/substrate/primitives/runtime/src/lib.rs index eb8bbb38a6ffe8dfeeaffb9a29fd754520edf11f..ee381d82bef817b734919aefac9a26a8c4c45646 100644 --- a/substrate/primitives/runtime/src/lib.rs +++ b/substrate/primitives/runtime/src/lib.rs @@ -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`") } } @@ -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"); + }); + } } diff --git a/substrate/primitives/std/with_std.rs b/substrate/primitives/std/with_std.rs index e1994e764d23e340c0332e500bd933aa84f6e017..92e804b27e1d028beb44f6b6f5d83deb0973240f 100644 --- a/substrate/primitives/std/with_std.rs +++ b/substrate/primitives/std/with_std.rs @@ -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; +} diff --git a/substrate/primitives/std/without_std.rs b/substrate/primitives/std/without_std.rs index 09f7a1976cc022078d0a5975ccc736cc2edb08a3..3c130d547a1e4a8972ff4b46b4d5e60d2fa07385 100755 --- a/substrate/primitives/std/without_std.rs +++ b/substrate/primitives/std/without_std.rs @@ -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 + } +}