Skip to content

Commit f607be8

Browse files
Handle Ctrl+C in the terminal properly (#14001)
# Objective Fixes #13995. ## Solution Override the default `Ctrl+C` handler with one that sends `AppExit` event to every app with `TerminalCtrlCHandlerPlugin`. ## Testing Tested by running the `3d_scene` example and hitting `Ctrl+C` in the terminal. --- ## Changelog Handles `Ctrl+C` in the terminal gracefully. ## Migration Guide If you are overriding the `Ctrl+C` handler then you should call `TerminalCtrlCHandlerPlugin::gracefully_exit` from your handler. It will tell the app to exit.
1 parent cb4fe4e commit f607be8

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

crates/bevy_app/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ bevy_tasks = { path = "../bevy_tasks", version = "0.14.0-dev" }
2626
downcast-rs = "1.2.0"
2727
thiserror = "1.0"
2828

29+
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
30+
ctrlc = "3.4.4"
31+
2932
[target.'cfg(target_arch = "wasm32")'.dependencies]
3033
wasm-bindgen = { version = "0.2" }
3134
web-sys = { version = "0.3", features = ["Window"] }

crates/bevy_app/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ mod plugin;
1414
mod plugin_group;
1515
mod schedule_runner;
1616
mod sub_app;
17+
#[cfg(not(target_arch = "wasm32"))]
18+
mod terminal_ctrl_c_handler;
1719

1820
pub use app::*;
1921
pub use bevy_derive::DynamicPlugin;
@@ -23,6 +25,8 @@ pub use plugin::*;
2325
pub use plugin_group::*;
2426
pub use schedule_runner::*;
2527
pub use sub_app::*;
28+
#[cfg(not(target_arch = "wasm32"))]
29+
pub use terminal_ctrl_c_handler::*;
2630

2731
#[allow(missing_docs)]
2832
pub mod prelude {
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use std::sync::atomic::{AtomicBool, Ordering};
2+
3+
use bevy_ecs::event::EventWriter;
4+
5+
use crate::{App, AppExit, Plugin, Update};
6+
7+
pub use ctrlc;
8+
9+
/// Indicates that all [`App`]'s should exit.
10+
static SHOULD_EXIT: AtomicBool = AtomicBool::new(false);
11+
12+
/// Gracefully handles `Ctrl+C` by emitting a [`AppExit`] event. This plugin is part of the `DefaultPlugins`.
13+
///
14+
/// ```no_run
15+
/// # use bevy_app::{App, NoopPluginGroup as MinimalPlugins, PluginGroup, TerminalCtrlCHandlerPlugin};
16+
/// fn main() {
17+
/// App::new()
18+
/// .add_plugins(MinimalPlugins)
19+
/// .add_plugins(TerminalCtrlCHandlerPlugin)
20+
/// .run();
21+
/// }
22+
/// ```
23+
///
24+
/// If you want to setup your own `Ctrl+C` handler, you should call the
25+
/// [`TerminalCtrlCHandlerPlugin::gracefully_exit`] function in your handler if you want bevy to gracefully exit.
26+
/// ```no_run
27+
/// # use bevy_app::{App, NoopPluginGroup as DefaultPlugins, PluginGroup, TerminalCtrlCHandlerPlugin, ctrlc};
28+
/// fn main() {
29+
/// // Your own `Ctrl+C` handler
30+
/// ctrlc::set_handler(move || {
31+
/// // Other clean up code ...
32+
///
33+
/// TerminalCtrlCHandlerPlugin::gracefully_exit();
34+
/// });
35+
///
36+
/// App::new()
37+
/// .add_plugins(DefaultPlugins)
38+
/// .run();
39+
/// }
40+
/// ```
41+
#[derive(Default)]
42+
pub struct TerminalCtrlCHandlerPlugin;
43+
44+
impl TerminalCtrlCHandlerPlugin {
45+
/// Sends the [`AppExit`] event to all apps using this plugin to make them gracefully exit.
46+
pub fn gracefully_exit() {
47+
SHOULD_EXIT.store(true, Ordering::Relaxed);
48+
}
49+
50+
/// Sends a [`AppExit`] event when the user presses `Ctrl+C` on the terminal.
51+
fn exit_on_flag(mut events: EventWriter<AppExit>) {
52+
if SHOULD_EXIT.load(Ordering::Relaxed) {
53+
events.send(AppExit::from_code(130));
54+
}
55+
}
56+
}
57+
58+
impl Plugin for TerminalCtrlCHandlerPlugin {
59+
fn build(&self, app: &mut App) {
60+
let result = ctrlc::try_set_handler(move || {
61+
Self::gracefully_exit();
62+
});
63+
match result {
64+
Ok(()) => {}
65+
Err(ctrlc::Error::MultipleHandlers) => {
66+
bevy_utils::tracing::info!("Skipping installing `Ctrl+C` handler as one was already installed. Please call `TerminalCtrlCHandlerPlugin::gracefully_exit` in your own `Ctrl+C` handler if you want Bevy to gracefully exit on `Ctrl+C`.");
67+
}
68+
Err(err) => bevy_utils::tracing::warn!("Failed to set `Ctrl+C` handler: {err}"),
69+
}
70+
71+
app.add_systems(Update, TerminalCtrlCHandlerPlugin::exit_on_flag);
72+
}
73+
}

crates/bevy_internal/src/default_plugins.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ impl PluginGroup for DefaultPlugins {
5959
.add(bevy_window::WindowPlugin::default())
6060
.add(bevy_a11y::AccessibilityPlugin);
6161

62+
#[cfg(not(target_arch = "wasm32"))]
63+
{
64+
group = group.add(bevy_app::TerminalCtrlCHandlerPlugin);
65+
}
66+
6267
#[cfg(feature = "bevy_asset")]
6368
{
6469
group = group.add(bevy_asset::AssetPlugin::default());

0 commit comments

Comments
 (0)