block signals early: now handled correctly with tracy ondemand

This commit is contained in:
sodiboo
2025-07-18 20:14:05 +02:00
committed by Ivan Molodetskikh
parent 8f442dee06
commit 485e667fec
2 changed files with 28 additions and 0 deletions
+3
View File
@@ -134,6 +134,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
}
// Needs to be done before starting Tracy, so that it applies to Tracy's threads.
niri::utils::signals::block_early().unwrap();
// Avoid starting Tracy for the `niri msg` code path since starting/stopping Tracy is a bit
// slow.
tracy_client::Client::start();
+25
View File
@@ -34,6 +34,12 @@ pub fn listen(handle: &calloop::LoopHandle<crate::niri::State>) {
.unwrap();
}
// We block the signals early, so that they apply to all threads.
// They are then blocked *again* by the `Signals` source. That's fine.
pub fn block_early() -> io::Result<()> {
set_sigmask(&preferred_sigset()?)
}
pub fn unblock_all() -> io::Result<()> {
set_sigmask(&empty_sigset()?)
}
@@ -47,6 +53,25 @@ pub fn empty_sigset() -> io::Result<libc::sigset_t> {
}
}
pub fn preferred_sigset() -> io::Result<libc::sigset_t> {
let mut set = empty_sigset()?;
unsafe {
add_signal(&mut set, libc::SIGINT)?;
add_signal(&mut set, libc::SIGTERM)?;
add_signal(&mut set, libc::SIGHUP)?;
}
Ok(set)
}
// SAFETY: `signum` must be a valid signal number.
unsafe fn add_signal(set: &mut libc::sigset_t, signum: libc::c_int) -> io::Result<()> {
if unsafe { libc::sigaddset(set, signum) } == 0 {
Ok(())
} else {
Err(io::Error::last_os_error())
}
}
pub fn set_sigmask(set: &libc::sigset_t) -> io::Result<()> {
let oldset = std::ptr::null_mut(); // ignore old mask
if unsafe { libc::pthread_sigmask(libc::SIG_SETMASK, set, oldset) } == 0 {