diff --git a/Cargo.lock b/Cargo.lock index db541c50..9d98cd69 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -168,7 +168,7 @@ version = "0.59.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2bd2a9a458e8f4304c52c43ebb0cfbd520289f8379a52e329a38afda99bf8eb8" dependencies = [ - "bitflags", + "bitflags 1.3.2", "cexpr", "clang-sys", "clap 2.34.0", @@ -191,7 +191,7 @@ version = "0.60.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "062dddbc1ba4aca46de6338e2bf87771414c335f7b2f2036e8f3e9befebf88e6" dependencies = [ - "bitflags", + "bitflags 1.3.2", "cexpr", "clang-sys", "clap 3.2.25", @@ -214,6 +214,12 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bitflags" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" + [[package]] name = "bumpalo" version = "3.13.0" @@ -253,6 +259,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cfg_aliases" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" + [[package]] name = "chrono" version = "0.4.26" @@ -287,7 +299,7 @@ checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" dependencies = [ "ansi_term", "atty", - "bitflags", + "bitflags 1.3.2", "strsim 0.8.0", "textwrap 0.11.0", "unicode-width", @@ -301,7 +313,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" dependencies = [ "atty", - "bitflags", + "bitflags 1.3.2", "clap_derive 3.2.25", "clap_lex 0.2.4", "indexmap", @@ -331,7 +343,7 @@ checksum = "72394f3339a76daf211e57d4bcb374410f3965dcc606dd0e03738c7888766980" dependencies = [ "anstream", "anstyle", - "bitflags", + "bitflags 1.3.2", "clap_lex 0.5.0", "strsim 0.10.0", ] @@ -989,9 +1001,9 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" [[package]] name = "libc" -version = "0.2.144" +version = "0.2.154" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1" +checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346" [[package]] name = "libloading" @@ -1158,7 +1170,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f346ff70e7dbfd675fe90590b92d59ef2de15a8779ae305ebcbfd3f0caf59be4" dependencies = [ "autocfg", - "bitflags", + "bitflags 1.3.2", "cfg-if", "libc", ] @@ -1169,12 +1181,24 @@ version = "0.26.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bfdda3d196821d6af13126e40375cdf7da646a96114af134d5f417a9a1dc8e1a" dependencies = [ - "bitflags", + "bitflags 1.3.2", "cfg-if", "libc", "static_assertions", ] +[[package]] +name = "nix" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab2156c4fce2f8df6c499cc1c763e4394b7482525bf2a9701c9d79d215f519e4" +dependencies = [ + "bitflags 2.5.0", + "cfg-if", + "cfg_aliases", + "libc", +] + [[package]] name = "nom" version = "7.1.3" @@ -1243,7 +1267,7 @@ version = "0.10.54" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69b3f656a17a6cbc115b5c7a40c616947d213ba182135b014d6051b73ab6f019" dependencies = [ - "bitflags", + "bitflags 1.3.2", "cfg-if", "foreign-types", "libc", @@ -1433,6 +1457,7 @@ dependencies = [ "log", "lru", "memmap", + "nix 0.28.0", "proc-maps 0.2.1", "py-spy-testdata", "rand", @@ -1443,6 +1468,7 @@ dependencies = [ "serde", "serde_derive", "serde_json", + "signal-hook", "tempfile", "termios", "tokio", @@ -1531,7 +1557,7 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" dependencies = [ - "bitflags", + "bitflags 1.3.2", ] [[package]] @@ -1540,7 +1566,7 @@ version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29" dependencies = [ - "bitflags", + "bitflags 1.3.2", ] [[package]] @@ -1647,7 +1673,7 @@ version = "0.37.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acf8729d8542766f1b2cf77eb034d52f40d375bb8b615d0b147089946e16613d" dependencies = [ - "bitflags", + "bitflags 1.3.2", "errno 0.3.1", "io-lifetimes", "libc", @@ -1702,7 +1728,7 @@ version = "2.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fc758eb7bffce5b308734e9b0c1468893cae9ff70ebf13e7090be8dcbcc83a8" dependencies = [ - "bitflags", + "bitflags 1.3.2", "core-foundation", "core-foundation-sys", "libc", @@ -1765,6 +1791,16 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3" +[[package]] +name = "signal-hook" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8621587d4798caf8eb44879d42e56b9a93ea5dcd315a6487c357130095b62801" +dependencies = [ + "libc", + "signal-hook-registry", +] + [[package]] name = "signal-hook-registry" version = "1.4.1" diff --git a/Cargo.toml b/Cargo.toml index bf248c74..aece43f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,6 +39,8 @@ remoteprocess = {version="0.4.11", features=["unwind"]} chrono = "0.4.19" reqwest = { version = "0.11", features = ["blocking"] } tokio = { version = "1", features = ["full"] } +signal-hook = "0.3.17" +nix = {version="0.28.0", features = ["signal"] } [dev-dependencies] py-spy-testdata = "0.1.0" diff --git a/src/main.rs b/src/main.rs index 8d232939..0a37733e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,7 +37,11 @@ use console::style; use config::{Config, FileFormat, RecordDuration}; use console_viewer::ConsoleViewer; +use libc::{SIGINT, SIGTERM}; use reqwest::StatusCode; +use signal_hook::iterator::Signals; +use nix::unistd::Pid; +use nix::sys::signal::{self, Signal}; use stack_trace::{Frame, StackTrace}; use chrono::{Local, SecondsFormat}; @@ -597,6 +601,15 @@ fn pyspy_main() -> Result<(), Error> { .spawn() .map_err(|e| format_err!("Failed to create process '{}': {}", subprocess[0], e))?; + let mut signals = Signals::new(&[SIGINT, SIGTERM])?; + let child_pid = command.id(); + std::thread::spawn(move || { + for sig in signals.forever() { + signal::kill(Pid::from_raw(child_pid.try_into().unwrap()), Signal::try_from(sig).unwrap()).unwrap(); + println!("Received signal {:?}", sig); + } + }); + #[cfg(target_os = "macos")] { // sleep just in case: https://jvns.ca/blog/2018/01/28/mac-freeze/ diff --git a/src/sampler.rs b/src/sampler.rs index 6d228539..7fb66d3d 100644 --- a/src/sampler.rs +++ b/src/sampler.rs @@ -56,7 +56,7 @@ impl Sampler { spy } Err(e) => { - initialized_tx.send(Err(e)).is_err(); + let _ = initialized_tx.send(Err(e)).is_err(); return; } }; @@ -308,7 +308,7 @@ impl PythonSpyThread { } Err(e) => { warn!("Failed to profile python from process {}: {}", pid, e); - initialized_tx.send(Err(e)).is_err(); + let _ = initialized_tx.send(Err(e)).is_err(); return; } };