Skip to content

Commit fa117de

Browse files
authored
Propagate panics in tasks (#78)
After smol-rs/async-task#37 I meant to add this to the executor. This commit makes it so all panics are surfaced in the tasks that the user calls. Hopefully this improves ergonomics. Signed-off-by: John Nunley <[email protected]> Signed-off-by: Alain Zscheile <[email protected]>
1 parent 4b1cf40 commit fa117de

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/lib.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use std::sync::{Arc, Mutex, RwLock, TryLockError};
4343
use std::task::{Poll, Waker};
4444

4545
use async_lock::OnceCell;
46-
use async_task::Runnable;
46+
use async_task::{Builder, Runnable};
4747
use concurrent_queue::ConcurrentQueue;
4848
use futures_lite::{future, prelude::*};
4949
use slab::Slab;
@@ -159,7 +159,11 @@ impl<'a> Executor<'a> {
159159
};
160160

161161
// Create the task and register it in the set of active tasks.
162-
let (runnable, task) = unsafe { async_task::spawn_unchecked(future, self.schedule()) };
162+
let (runnable, task) = unsafe {
163+
Builder::new()
164+
.propagate_panic(true)
165+
.spawn_unchecked(|()| future, self.schedule())
166+
};
163167
active.insert(runnable.waker());
164168

165169
runnable.schedule();
@@ -402,7 +406,11 @@ impl<'a> LocalExecutor<'a> {
402406
};
403407

404408
// Create the task and register it in the set of active tasks.
405-
let (runnable, task) = unsafe { async_task::spawn_unchecked(future, self.schedule()) };
409+
let (runnable, task) = unsafe {
410+
Builder::new()
411+
.propagate_panic(true)
412+
.spawn_unchecked(|()| future, self.schedule())
413+
};
406414
active.insert(runnable.waker());
407415

408416
runnable.schedule();

tests/panic_prop.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use async_executor::Executor;
2+
use futures_lite::{future, prelude::*};
3+
4+
#[test]
5+
fn test_panic_propagation() {
6+
let ex = Executor::new();
7+
let task = ex.spawn(async { panic!("should be caught by the task") });
8+
9+
// Running the executor should not panic.
10+
assert!(ex.try_tick());
11+
12+
// Polling the task should.
13+
assert!(future::block_on(task.catch_unwind()).is_err());
14+
}

0 commit comments

Comments
 (0)