-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[Feature] Improved Panic Handling #2927
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
base: staging
Are you sure you want to change the base?
Changes from all commits
55a5889
938877d
f104f5a
b90ef1e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,9 +34,6 @@ pub use bytes::*; | |
| pub mod defer; | ||
| pub use defer::*; | ||
|
|
||
| mod vm_error; | ||
| pub use vm_error::*; | ||
|
|
||
| pub mod iterator; | ||
| pub use iterator::*; | ||
|
|
||
|
|
@@ -58,5 +55,24 @@ pub use serialize::*; | |
| pub mod errors; | ||
| pub use errors::*; | ||
|
|
||
| #[cfg(feature = "async")] | ||
| /// Helpers to spawn async tasks. | ||
| pub mod task; | ||
| #[cfg(feature = "async")] | ||
| pub use task::*; | ||
|
|
||
| /// Use old name for backward-compatibility. | ||
| pub use errors::io_error as error; | ||
|
|
||
| /// This macro provides a VM runtime environment which will safely halt | ||
| /// without producing logs that look like unexpected behavior. | ||
| /// In debug mode, it prints to stderr using the format: "VM safely halted at {location}: {halt message}". | ||
| /// | ||
| /// It is more efficient to set the panic hook once and directly use `errors::try_vm_runtime`. | ||
| #[macro_export] | ||
| macro_rules! try_vm_runtime { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't all the finalize instances still call the macro This would call
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I clarified the documentation that it is safe to call It would be great if we could replace that macro eventually, but that would require users of snarkVM to call |
||
| ($e:expr) => {{ | ||
| $crate::errors::set_panic_hook(); | ||
| $crate::errors::try_vm_runtime($e) | ||
| }}; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Copyright (c) 2019-2025 Provable Inc. | ||
| // This file is part of the snarkVM library. | ||
|
|
||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at: | ||
|
|
||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| use std::{ | ||
| future::Future, | ||
| pin::Pin, | ||
| task::{Context, Poll}, | ||
| }; | ||
|
|
||
| /// Wrapper around `tokio::JoinHandle` that propagates panics. | ||
| pub struct JoinHandle<R: Send + 'static> { | ||
| inner: tokio::task::JoinHandle<R>, | ||
| } | ||
|
|
||
| /// Wrapper around `tokio::spawn_blocking` that propagates panics. | ||
| pub fn spawn_blocking<F, R>(f: F) -> JoinHandle<R> | ||
| where | ||
| F: FnOnce() -> R + Send + 'static, | ||
| R: Send + 'static, | ||
| { | ||
| JoinHandle { inner: tokio::task::spawn_blocking(f) } | ||
| } | ||
|
|
||
| /// Wrapper around `tokio::spawn` that propagates panics. | ||
| pub fn spawn<F>(f: F) -> JoinHandle<F::Output> | ||
| where | ||
| F: Future + Send + 'static, | ||
| F::Output: Send + 'static, | ||
| { | ||
| JoinHandle { inner: tokio::task::spawn(f) } | ||
| } | ||
|
|
||
| impl<R: Send + 'static> JoinHandle<R> { | ||
| pub fn abort(&self) { | ||
| self.inner.abort(); | ||
| } | ||
| } | ||
|
|
||
| impl<R: Send + 'static> Future for JoinHandle<R> { | ||
| type Output = R; | ||
|
|
||
| fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
| let Poll::Ready(result) = std::pin::pin!(&mut self.inner).poll(cx) else { | ||
| return Poll::Pending; | ||
| }; | ||
|
|
||
| match result { | ||
| Ok(value) => Poll::Ready(value), | ||
| Err(err) => { | ||
| if err.is_panic() { | ||
| // Resume the panic on the main task | ||
vicsn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| std::panic::resume_unwind(err.into_panic()); | ||
vicsn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } else { | ||
| panic!("Got unexpected tokio error: {err}"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.