servoshell: Port from sig to signal_hook_registry (#43891)

Testing: We do not currently have a way to test signal handling in the
servoshell binary, so this change does not include tests.
Fixes: #43836

---------

Signed-off-by: Narfinger <Narfinger@users.noreply.github.com>
This commit is contained in:
Narfinger
2026-04-07 16:28:36 +08:00
committed by GitHub
parent 7a559ba459
commit 8a38c5e217
3 changed files with 16 additions and 19 deletions

11
Cargo.lock generated
View File

@@ -8978,7 +8978,7 @@ dependencies = [
"servo-allocator",
"servo-base",
"servo-webdriver-server",
"sig",
"signal-hook-registry",
"surfman",
"tokio",
"tracing",
@@ -9044,15 +9044,6 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "sig"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6567e29578f9bfade6a5d94a32b9a4256348358d2a3f448cab0021f9a02614a2"
dependencies = [
"libc",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.8"

View File

@@ -130,7 +130,7 @@ surfman = { workspace = true, features = ["sm-raw-window-handle-06", "sm-x11"] }
winit = { workspace = true }
[target.'cfg(any(all(target_os = "linux", not(target_env = "ohos")), target_os = "macos"))'.dependencies]
sig = "1.0"
signal-hook-registry = "1.4.8"
[target.'cfg(target_os = "windows")'.dependencies]
servo = { workspace = true, features = ["no-wgl"] }

View File

@@ -11,11 +11,11 @@ pub fn install() {
use std::sync::atomic;
use std::thread;
use sig::signal;
use libc::siginfo_t;
use crate::backtrace;
extern "C" fn handler(sig: i32) {
fn handler(siginfo: &siginfo_t) {
// Only print crash message and backtrace the first time, to avoid
// infinite recursion if the printing causes another signal.
static BEEN_HERE_BEFORE: atomic::AtomicBool = atomic::AtomicBool::new(false);
@@ -26,7 +26,7 @@ pub fn install() {
// practice will often segfault (see below).
let stderr = std::io::stderr();
let mut stderr = stderr.lock();
let _ = write!(&mut stderr, "Caught signal {sig}");
let _ = write!(&mut stderr, "Caught signal {}", siginfo.si_signo);
if let Some(name) = thread::current().name() {
let _ = write!(&mut stderr, " in thread \"{}\"", name);
}
@@ -42,13 +42,19 @@ pub fn install() {
// know to be “async-signal-safe”, which includes sigaction(), raise(),
// and _exit(), but generally doesnt include anything that allocates.
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_04_03_03
raise_signal_or_exit_with_error(sig);
raise_signal_or_exit_with_error(siginfo.si_signo);
}
signal!(libc::SIGSEGV, handler); // handle segfaults
signal!(libc::SIGILL, handler); // handle stack overflow and unsupported CPUs
signal!(libc::SIGIOT, handler); // handle double panics
signal!(libc::SIGBUS, handler); // handle invalid memory access
unsafe {
signal_hook_registry::register_unchecked(libc::SIGSEGV, handler)
.expect("Could not register SIGSEGV handler");
signal_hook_registry::register_unchecked(libc::SIGILL, handler)
.expect("Could not register SIGILL handler");
signal_hook_registry::register_unchecked(libc::SIGIOT, handler)
.expect("Could not register SIGIOT handler");
signal_hook_registry::register_unchecked(libc::SIGBUS, handler)
.expect("Could not register SIGBUS handler");
}
}
#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "android")))]