Skip to content

Commit e326796

Browse files
committed
run executors forever even with panics
1 parent cee9c53 commit e326796

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

crates/bevy_tasks/src/task_pool.rs

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::{
22
future::Future,
33
marker::PhantomData,
44
mem,
5+
panic::AssertUnwindSafe,
56
sync::Arc,
67
thread::{self, JoinHandle},
78
};
@@ -300,19 +301,54 @@ impl TaskPool {
300301

301302
let tick_task_pool_executor = tick_task_pool_executor || self.threads.is_empty();
302303
if let Some(thread_ticker) = thread_executor.ticker() {
303-
let tick_forever = async move {
304-
loop {
305-
thread_ticker.tick().await;
306-
}
307-
};
308-
309304
if tick_task_pool_executor {
310-
executor.run(tick_forever).or(get_results).await
305+
let execute_forever = async move {
306+
// we restart the executors if a task errors. if a scoped
307+
// task errors it will panic the scope on the call to get_results
308+
loop {
309+
let tick_forever = async {
310+
loop {
311+
thread_ticker.tick().await;
312+
}
313+
};
314+
315+
// we don't care if it errors. If a scoped task errors it will propagate
316+
// to get_results
317+
let _result = AssertUnwindSafe(executor.run(tick_forever))
318+
.catch_unwind()
319+
.await
320+
.is_ok();
321+
}
322+
};
323+
execute_forever.or(get_results).await
311324
} else {
312-
tick_forever.or(get_results).await
325+
let execute_forever = async {
326+
loop {
327+
let tick_forever = async {
328+
loop {
329+
thread_ticker.tick().await;
330+
}
331+
};
332+
333+
let _result =
334+
AssertUnwindSafe(tick_forever).catch_unwind().await.is_ok();
335+
}
336+
};
337+
338+
execute_forever.or(get_results).await
313339
}
314340
} else if tick_task_pool_executor {
315-
executor.run(get_results).await
341+
let execute_forever = async {
342+
loop {
343+
let _result =
344+
AssertUnwindSafe(executor.run(std::future::pending::<()>()))
345+
.catch_unwind()
346+
.await
347+
.is_ok();
348+
}
349+
};
350+
351+
execute_forever.or(get_results).await
316352
} else {
317353
get_results.await
318354
}

0 commit comments

Comments
 (0)