Skip to content

Commit 5fc894f

Browse files
committed
Fix a deadlock that can occur when using scope() on ComputeTaskPool from within a system.
1 parent 4fecb89 commit 5fc894f

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

crates/bevy_tasks/src/task_pool.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,18 @@ impl TaskPool {
194194
let fut: Pin<&'static mut (dyn Future<Output = Vec<T>> + Send + 'static)> =
195195
unsafe { mem::transmute(fut) };
196196

197-
future::block_on(self.executor.spawn(fut))
197+
// The thread that calls scope() will participate in driving tasks in the pool forward
198+
// until the tasks that are spawned by this scope() call complete. (If the caller of scope()
199+
// happens to be a thread in this thread pool, and we only have one thread in the pool, then
200+
// simply calling future::block_on(spawned) would deadlock.)
201+
let mut spawned = self.executor.spawn(fut);
202+
loop {
203+
if let Some(result) = future::block_on(future::poll_once(&mut spawned)) {
204+
break result;
205+
}
206+
207+
self.executor.try_tick();
208+
}
198209
}
199210

200211
/// Spawns a static future onto the thread pool. The returned Task is a future. It can also be

0 commit comments

Comments
 (0)