Skip to content
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

Add unwind feature flag #3211

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tokio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ stream = ["futures-core"]
sync = []
test-util = []
time = []
unwind = []

[dependencies]
tokio-macros = { version = "0.3.0", path = "../tokio-macros", optional = true }
Expand Down
35 changes: 27 additions & 8 deletions tokio/src/runtime/task/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,18 @@ where
// future has been obtained. This also ensures the `*mut T` pointer
// contains the future (as opposed to the output) and is initialized.

let res = panic::catch_unwind(panic::AssertUnwindSafe(|| {
struct Guard<'a, T: Future, S: Schedule> {
core: &'a Core<T, S>,
}
struct Guard<'a, T: Future, S: Schedule> {
core: &'a Core<T, S>,
}

impl<T: Future, S: Schedule> Drop for Guard<'_, T, S> {
fn drop(&mut self) {
self.core.drop_future_or_output();
}
impl<T: Future, S: Schedule> Drop for Guard<'_, T, S> {
fn drop(&mut self) {
self.core.drop_future_or_output();
}
}

#[cfg(not(feature = "unwind"))]
let res = panic::catch_unwind(panic::AssertUnwindSafe(|| {
let guard = Guard { core: self.core() };

// If the task is cancelled, avoid polling it, instead signalling it
Expand All @@ -113,6 +114,24 @@ where
}
}));

#[cfg(feature = "unwind")]
let res = Ok({
let guard = Guard { core: self.core() };

// If the task is cancelled, avoid polling it, instead signalling it
// is complete.
if snapshot.is_cancelled() {
Poll::Ready(Err(JoinError::cancelled()))
} else {
let res = guard.core.poll(self.header());

// prevent the guard from dropping the future
mem::forget(guard);

res.map(Ok)
}
});

match res {
Ok(Poll::Ready(out)) => {
self.complete(out, snapshot.is_join_interested());
Expand Down
18 changes: 18 additions & 0 deletions tokio/tests/panics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#[cfg(feature = "unwind")]
#[tokio::test]
#[should_panic]
async fn panic_propagated() {
tokio::spawn(async {
panic!();
});
tokio::task::yield_now().await;
}

#[cfg(not(feature = "unwind"))]
#[tokio::test]
async fn panic_not_propagated() {
tokio::spawn(async {
panic!();
});
tokio::task::yield_now().await;
}
1 change: 1 addition & 0 deletions tokio/tests/process_issue_2174.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use tokio::process::Command;
use tokio::time;
use tokio_test::assert_err;

#[cfg(not(feature = "unwind"))]
#[tokio::test]
async fn issue_2174() {
let mut child = Command::new("sleep")
Expand Down
1 change: 1 addition & 0 deletions tokio/tests/rt_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ rt_test! {
assert_err!(rx.try_recv());
}

#[cfg(not(feature = "unwind"))]
#[test]
fn panic_in_task() {
let rt = rt();
Expand Down