Skip to content

Commit

Permalink
Fix a deadlock that can occur when using scope() on ComputeTaskPool f…
Browse files Browse the repository at this point in the history
…rom within a system.
  • Loading branch information
aclysma authored and cart committed Nov 26, 2020
1 parent bd5bd3b commit bacd175
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion crates/bevy_tasks/src/task_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,18 @@ impl TaskPool {
let fut: Pin<&'static mut (dyn Future<Output = Vec<T>> + Send + 'static)> =
unsafe { mem::transmute(fut) };

future::block_on(self.executor.spawn(fut))
// The thread that calls scope() will participate in driving tasks in the pool forward
// until the tasks that are spawned by this scope() call complete. (If the caller of scope()
// happens to be a thread in this thread pool, and we only have one thread in the pool, then
// simply calling future::block_on(spawned) would deadlock.)
let mut spawned = self.executor.spawn(fut);
loop {
if let Some(result) = future::block_on(future::poll_once(&mut spawned)) {
break result;
}

self.executor.try_tick();
}
}

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

0 comments on commit bacd175

Please sign in to comment.