Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Ctrl+C in the terminal properly #14001

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

SarthakSingh31
Copy link
Contributor

@SarthakSingh31 SarthakSingh31 commented Jun 24, 2024

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.

@SarthakSingh31 SarthakSingh31 force-pushed the sar/issue-13995 branch 3 times, most recently from f3a017f to e5c89a3 Compare June 24, 2024 14:47
#[cfg(not(target_arch = "wasm32"))]
impl TerminalCtrlCHandlerPlugin {
/// Handler for when the user presses `Ctrl+C` on the terminal. Should be called from the `Ctrl+C` hook.
pub fn on_ctrl_c() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason this is public? I'd rather hide it if not.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See their migration guide. If I understand it correctly, the idea is that you can still call this manually to notify all apps even when you're doing your own, manual CTRL-C handling.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed it but the idea was that if a user defines their own ctrlc handler then they will be able to use the plugins exit function to in that handler to exit bevy. They would not have to setup their own system and static flag.

fn build(&self, app: &mut App) {
#[cfg(not(target_arch = "wasm32"))]
{
ctrlc::set_handler(move || {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you use try_set_handler instead, and handle errors to log if there's already one set instead of panicking?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set_handler always overwrites the current handler so it should not panic.
But I am changing it to try_set_handle so that the user can set their own handler. They can then call our TerminalCtrlCHandlerPlugin::gracefully_exit in their handler to exit bevy gracefully.

@mockersf
Copy link
Member

should this be behind a feature?

@JMS55
Copy link
Contributor

JMS55 commented Jun 24, 2024

Imo, no. We already have a lot of features and have issues testing every combination. Users can just disable the plugin if they don't want it. I guess they'd still have a dependency on ctrlc though, but eh.

@janhohenheim janhohenheim added C-Enhancement A new feature A-Windowing Platform-agnostic interface layer to run your app in A-App Bevy apps and plugins S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Jun 25, 2024
@SarthakSingh31 SarthakSingh31 force-pushed the sar/issue-13995 branch 2 times, most recently from 0d09884 to 214f06a Compare June 25, 2024 06:40
crates/bevy_app/src/terminal_ctrl_c_handler.rs Outdated Show resolved Hide resolved
crates/bevy_app/src/terminal_ctrl_c_handler.rs Outdated Show resolved Hide resolved
crates/bevy_app/src/terminal_ctrl_c_handler.rs Outdated Show resolved Hide resolved
examples/3d/3d_scene.rs Outdated Show resolved Hide resolved
crates/bevy_app/src/terminal_ctrl_c_handler.rs Outdated Show resolved Hide resolved
@janhohenheim
Copy link
Member

Looks mostly good to me, left some minor notes. I don't like that fact that the graceful shutdown is broadcast globally, but it shouldn't matter in practice.

@SarthakSingh31 SarthakSingh31 force-pushed the sar/issue-13995 branch 2 times, most recently from e0ea791 to 1ce1958 Compare June 25, 2024 09:29
@janhohenheim
Copy link
Member

One last rephrase, then I'm good :)

Copy link
Member

@janhohenheim janhohenheim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM now :)
FYI, the PR is squashed anyways, so you don't need to force push your changes during a review to keep the history clean.

@janhohenheim janhohenheim added S-Needs-Review Needs reviewer attention (from anyone!) to move forward and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-App Bevy apps and plugins A-Windowing Platform-agnostic interface layer to run your app in C-Enhancement A new feature S-Needs-Review Needs reviewer attention (from anyone!) to move forward
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bevy should capture Ctrl+C, send AppExit event
5 participants