Skip to content

Propagate panics in tasks #78

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

Merged
merged 2 commits into from
Nov 21, 2023
Merged
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
14 changes: 11 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use std::sync::{Arc, Mutex, RwLock, TryLockError};
use std::task::{Poll, Waker};

use async_lock::OnceCell;
use async_task::Runnable;
use async_task::{Builder, Runnable};
use concurrent_queue::ConcurrentQueue;
use futures_lite::{future, prelude::*};
use slab::Slab;
Expand Down Expand Up @@ -159,7 +159,11 @@ impl<'a> Executor<'a> {
};

// Create the task and register it in the set of active tasks.
let (runnable, task) = unsafe { async_task::spawn_unchecked(future, self.schedule()) };
let (runnable, task) = unsafe {
Builder::new()
.propagate_panic(true)
.spawn_unchecked(|()| future, self.schedule())
};
active.insert(runnable.waker());

runnable.schedule();
Expand Down Expand Up @@ -402,7 +406,11 @@ impl<'a> LocalExecutor<'a> {
};

// Create the task and register it in the set of active tasks.
let (runnable, task) = unsafe { async_task::spawn_unchecked(future, self.schedule()) };
let (runnable, task) = unsafe {
Builder::new()
.propagate_panic(true)
.spawn_unchecked(|()| future, self.schedule())
};
active.insert(runnable.waker());

runnable.schedule();
Expand Down
14 changes: 14 additions & 0 deletions tests/panic_prop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use async_executor::Executor;
use futures_lite::{future, prelude::*};

#[test]
fn test_panic_propagation() {
let ex = Executor::new();
let task = ex.spawn(async { panic!("should be caught by the task") });

// Running the executor should not panic.
assert!(ex.try_tick());

// Polling the task should.
assert!(future::block_on(task.catch_unwind()).is_err());
}