-
Notifications
You must be signed in to change notification settings - Fork 226
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
feat: impl spawn_catch_unwind() #319
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -351,9 +351,6 @@ impl From<Runtime<TimeDriver<IoUringDriver>>> for FusionRuntime<TimeDriver<IoUri | |
/// | ||
/// # Examples | ||
/// | ||
/// In this example, a server is started and `spawn` is used to start a new task | ||
/// that processes each received connection. | ||
/// | ||
/// ```no_run | ||
/// #[monoio::main] | ||
/// async fn main() { | ||
|
@@ -382,6 +379,40 @@ where | |
join | ||
} | ||
|
||
/// Similar to [`spawn()`], but it catches any panic that occurs in the spawned task. | ||
/// | ||
/// Note that `.await`ing returned `JoinHandle` now returns a `Result<T::Output>` | ||
/// rather than `T::Output`. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// #[monoio::main] | ||
/// async fn main() { | ||
/// let handle = monoio::spawn_catch_unwind(async { | ||
/// println!("hello from a background task"); | ||
/// }); | ||
/// | ||
/// // Let the task complete | ||
/// handle.await.expect("no panic would happen!"); | ||
/// } | ||
/// ``` | ||
pub fn spawn_catch_unwind<T>( | ||
future: T, | ||
) -> JoinHandle<Result<T::Output, Box<dyn std::any::Any + 'static>>> | ||
where | ||
T: Future + 'static, | ||
T::Output: 'static, | ||
{ | ||
use futures::{FutureExt, TryFutureExt}; | ||
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. Using 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.
Yeah, this makes more sense 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. Could you change in any of these 2 ways? 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.
Yeah, I will, I am just busy with my work.
Get it. |
||
|
||
let future = std::panic::AssertUnwindSafe(future) | ||
.catch_unwind() | ||
.map_err(|e| e as Box<dyn std::any::Any + 'static>); | ||
|
||
spawn(future) | ||
} | ||
|
||
#[cfg(feature = "sync")] | ||
unsafe fn spawn_without_static<T>(future: T) -> JoinHandle<T::Output> | ||
where | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a copy-paste error, since the provided example is not a TCP server, let's just remove this sentence.