From af429936f0c7407f0c9797bcc24f9984a8f4d109 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Thu, 25 May 2023 11:08:02 +0200 Subject: [PATCH] Replace `ctrlc` crate with `tokio` (#2207) * Replace ctrlc crate with tokio signals * Don't treat SIGTERM as a crash --- Cargo.lock | 12 +----------- Cargo.toml | 1 - crates/rerun/src/crash_handler.rs | 1 - rerun_py/Cargo.toml | 3 +-- rerun_py/src/python_bridge.rs | 22 ++++++++++++---------- 5 files changed, 14 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d0153d259f58..46d8878270a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1200,16 +1200,6 @@ dependencies = [ "syn 1.0.109", ] -[[package]] -name = "ctrlc" -version = "3.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b37feaa84e6861e00a1f5e5aa8da3ee56d605c9992d33e082786754828e20865" -dependencies = [ - "nix 0.24.3", - "winapi", -] - [[package]] name = "d3d12" version = "0.6.0" @@ -4465,7 +4455,6 @@ name = "rerun_py" version = "0.6.0-alpha.0" dependencies = [ "arrow2", - "ctrlc", "document-features", "glam", "image", @@ -5241,6 +5230,7 @@ dependencies = [ "mio", "num_cpus", "pin-project-lite", + "signal-hook-registry", "socket2", "tokio-macros", "windows-sys 0.48.0", diff --git a/Cargo.toml b/Cargo.toml index 597545e8b3e7..f136cd2a734e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -61,7 +61,6 @@ cfg-if = "1.0" clap = "4.0" comfy-table = { version = "6.1", default-features = false } crossbeam = "0.8" -ctrlc = { version = "3.0", features = ["termination"] } ecolor = "0.22.0" eframe = { version = "0.22.0", default-features = false } egui = { version = "0.22.0", features = ["extra_debug_asserts", "log"] } diff --git a/crates/rerun/src/crash_handler.rs b/crates/rerun/src/crash_handler.rs index af5a2438e6d5..03f40e761453 100644 --- a/crates/rerun/src/crash_handler.rs +++ b/crates/rerun/src/crash_handler.rs @@ -123,7 +123,6 @@ fn install_signal_handler(build_info: BuildInfo) { libc::SIGFPE, libc::SIGILL, libc::SIGSEGV, - libc::SIGTERM, ] { // SAFETY: we're installing a signal handler. unsafe { diff --git a/rerun_py/Cargo.toml b/rerun_py/Cargo.toml index 6f3999c20677..08058ed45a17 100644 --- a/rerun_py/Cargo.toml +++ b/rerun_py/Cargo.toml @@ -49,7 +49,6 @@ re_web_viewer_server = { workspace = true, optional = true } re_ws_comms = { workspace = true, optional = true } arrow2 = { workspace = true, features = ["io_ipc", "io_print"] } -ctrlc.workspace = true document-features = "0.2" glam.workspace = true image = { workspace = true, default-features = false, features = [ @@ -64,7 +63,7 @@ once_cell = "1.12" parking_lot = "0.12" pyo3 = { version = "0.18.0", features = ["abi3-py38"] } rand = { version = "0.8", features = ["std_rng"] } -tokio = { workspace = true, features = ["rt-multi-thread"] } +tokio = { workspace = true, features = ["rt-multi-thread", "signal"] } uuid = "1.1" diff --git a/rerun_py/src/python_bridge.rs b/rerun_py/src/python_bridge.rs index 3a24bdc87a66..0dc3bdd33e42 100644 --- a/rerun_py/src/python_bridge.rs +++ b/rerun_py/src/python_bridge.rs @@ -63,22 +63,24 @@ fn global_web_viewer_server( #[pyfunction] fn main(py: Python<'_>, argv: Vec) -> PyResult { - // Python catches SIGINT and waits for us to release the GIL before shtting down. - // That's no good, so we need to catch SIGINT ourselves and shut down: - ctrlc::set_handler(move || { - eprintln!("Ctrl-C detected in rerun_py. Shutting down."); - #[allow(clippy::exit)] - std::process::exit(42); - }) - .expect("Error setting Ctrl-C handler"); - let build_info = re_build_info::build_info!(); let call_src = rerun::CallSource::Python(python_version(py)); tokio::runtime::Builder::new_multi_thread() .enable_all() .build() .unwrap() - .block_on(rerun::run(build_info, call_src, argv)) + .block_on(async { + // Python catches SIGINT and waits for us to release the GIL before shtting down. + // That's no good, so we need to catch SIGINT ourselves and shut down: + tokio::spawn(async move { + tokio::signal::ctrl_c().await.unwrap(); + eprintln!("Ctrl-C detected in rerun_py. Shutting down."); + #[allow(clippy::exit)] + std::process::exit(42); + }); + + rerun::run(build_info, call_src, argv).await + }) .map_err(|err| PyRuntimeError::new_err(re_error::format(err))) }