Skip to content

Commit

Permalink
close the channel so executor doesn't deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
hymm committed Feb 3, 2023
1 parent 27e20df commit 3e79c65
Showing 1 changed file with 43 additions and 17 deletions.
60 changes: 43 additions & 17 deletions crates/bevy_ecs/src/schedule_v3/executor/multi_threaded.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::panic::AssertUnwindSafe;

use bevy_tasks::{ComputeTaskPool, Scope, TaskPool};
use bevy_utils::default;
use bevy_utils::syncunsafecell::SyncUnsafeCell;
Expand Down Expand Up @@ -167,7 +169,7 @@ impl SystemExecutor for MultiThreadedExecutor {
.receiver
.recv()
.await
.unwrap_or_else(|error| unreachable!("{}", error));
.expect("A system has panicked so the executor cannot continue.");

self.finish_system_and_signal_dependents(index);

Expand Down Expand Up @@ -416,14 +418,22 @@ impl MultiThreadedExecutor {
let task = async move {
#[cfg(feature = "trace")]
let system_guard = system_span.enter();
// SAFETY: access is compatible
unsafe { system.run_unsafe((), world) };
let res = std::panic::catch_unwind(AssertUnwindSafe(|| {
// SAFETY: access is compatible
unsafe { system.run_unsafe((), world) };
}));
#[cfg(feature = "trace")]
drop(system_guard);
sender
.send(system_index)
.await
.unwrap_or_else(|error| unreachable!("{}", error));
if res.is_err() {
// close the channel to propagate the error to the
// multithreaded executor
sender.close();
} else {
sender
.send(system_index)
.await
.unwrap_or_else(|error| unreachable!("{}", error));
}
};

#[cfg(feature = "trace")]
Expand Down Expand Up @@ -466,13 +476,21 @@ impl MultiThreadedExecutor {
let task = async move {
#[cfg(feature = "trace")]
let system_guard = system_span.enter();
apply_system_buffers(&unapplied_systems, systems, world);
let res = std::panic::catch_unwind(AssertUnwindSafe(|| {
apply_system_buffers(&unapplied_systems, systems, world);
}));
#[cfg(feature = "trace")]
drop(system_guard);
sender
.send(system_index)
.await
.unwrap_or_else(|error| unreachable!("{}", error));
if res.is_err() {
// close the channel to propagate the error to the
// multithreaded executor
sender.close();
} else {
sender
.send(system_index)
.await
.unwrap_or_else(|error| unreachable!("{}", error));
}
};

#[cfg(feature = "trace")]
Expand All @@ -482,13 +500,21 @@ impl MultiThreadedExecutor {
let task = async move {
#[cfg(feature = "trace")]
let system_guard = system_span.enter();
system.run((), world);
let res = std::panic::catch_unwind(AssertUnwindSafe(|| {
system.run((), world);
}));
#[cfg(feature = "trace")]
drop(system_guard);
sender
.send(system_index)
.await
.unwrap_or_else(|error| unreachable!("{}", error));
if res.is_err() {
// close the channel to propagate the error to the
// multithreaded executor
sender.close();
} else {
sender
.send(system_index)
.await
.unwrap_or_else(|error| unreachable!("{}", error));
}
};

#[cfg(feature = "trace")]
Expand Down

0 comments on commit 3e79c65

Please sign in to comment.