Skip to content

Commit c23bb87

Browse files
committed
fixup new unsafe interrupt handler
1 parent 59b8104 commit c23bb87

8 files changed

+33
-18
lines changed

gix/examples/clone.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ fn main() -> anyhow::Result<()> {
1111
.nth(2)
1212
.context("The second argument is the directory to clone the repository into")?;
1313

14-
gix::interrupt::init_handler(1, || {})?;
14+
// SAFETY: The closure doesn't use mutexes or memory allocation, so it should be safe to call from a signal handler.
15+
unsafe {
16+
gix::interrupt::init_handler(1, || {})?;
17+
}
1518
std::fs::create_dir_all(&dst)?;
1619
let url = gix::url::parse(repo_url.to_str().unwrap().into())?;
1720

gix/examples/interrupt-handler-allows-graceful-shutdown.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ fn main() -> anyhow::Result<()> {
66
#[cfg(feature = "interrupt")]
77
fn main() -> anyhow::Result<()> {
88
use gix_tempfile::{AutoRemove, ContainingDirectory};
9-
gix::interrupt::init_handler(1, || {})?;
9+
// SAFETY: The closure doesn't use mutexes or memory allocation, so it should be safe to call from a signal handler.
10+
unsafe {
11+
gix::interrupt::init_handler(1, || {})?;
12+
}
1013
eprintln!("About to emit the first term signal");
1114
let tempfile_path = std::path::Path::new("example-file.tmp");
1215
let _keep_tempfile = gix_tempfile::mark_at(tempfile_path, ContainingDirectory::Exists, AutoRemove::Tempfile)?;

gix/examples/reversible-interrupt-handlers.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ fn main() -> anyhow::Result<()> {
66
#[cfg(feature = "interrupt")]
77
fn main() -> anyhow::Result<()> {
88
{
9-
let _deregister_on_drop = gix::interrupt::init_handler(1, || {})?.auto_deregister();
9+
// SAFETY: The closure doesn't use mutexes or memory allocation, so it should be safe to call from a signal handler.
10+
let _deregister_on_drop = unsafe { gix::interrupt::init_handler(1, || {})?.auto_deregister() };
1011
}
1112
eprintln!("About to emit the first term signal, which acts just like a normal one");
1213
signal_hook::low_level::raise(signal_hook::consts::SIGTERM)?;

gix/src/interrupt.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ mod init {
9090
/// It will abort the process on second press and won't inform the user about this behaviour either as we are unable to do so without
9191
/// deadlocking even when trying to write to stderr directly.
9292
///
93-
/// SAFETY: `interrupt()` will be called from a signal handler. See `signal_hook::low_level::register()` for details about.
94-
#[allow(unsafe_code)]
93+
/// SAFETY: `interrupt()` will be called from a signal handler. See [`signal_hook::low_level::register()`] for details about.
94+
#[allow(unsafe_code, clippy::missing_safety_doc)]
9595
pub unsafe fn init_handler(
9696
grace_count: usize,
9797
interrupt: impl Fn() + Send + Sync + Clone + 'static,

gix/tests/interrupt.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ mod needs_feature {
99
static V1: AtomicUsize = AtomicUsize::new(0);
1010
static V2: AtomicBool = AtomicBool::new(false);
1111

12-
let reg1 = gix::interrupt::init_handler(3, || {
13-
V1.fetch_add(1, Ordering::SeqCst);
14-
})
12+
// SAFETY: The closure doesn't use mutexes or memory allocation, so it should be safe to call from a signal handler.
13+
let reg1 = unsafe {
14+
gix::interrupt::init_handler(3, || {
15+
V1.fetch_add(1, Ordering::SeqCst);
16+
})
17+
}
1518
.expect("succeeds");
1619
assert!(!gix::interrupt::is_triggered());
1720
assert_eq!(V1.load(Ordering::Relaxed), 0);
18-
let reg2 =
19-
gix::interrupt::init_handler(2, || V2.store(true, Ordering::SeqCst)).expect("multi-initialization is OK");
21+
// SAFETY: The closure doesn't use mutexes or memory allocation, so it should be safe to call from a signal handler.
22+
let reg2 = unsafe { gix::interrupt::init_handler(2, || V2.store(true, Ordering::SeqCst)) }
23+
.expect("multi-initialization is OK");
2024
assert!(!V2.load(Ordering::Relaxed));
2125

2226
signal_hook::low_level::raise(SIGTERM).expect("signal can be raised");
@@ -36,13 +40,17 @@ mod needs_feature {
3640
"the deregistration succeeded and this is an optional side-effect"
3741
);
3842

39-
let reg1 = gix::interrupt::init_handler(3, || {
40-
V1.fetch_add(1, Ordering::SeqCst);
41-
})
43+
// SAFETY: The closure doesn't use mutexes or memory allocation, so it should be safe to call from a signal handler.
44+
let reg1 = unsafe {
45+
gix::interrupt::init_handler(3, || {
46+
V1.fetch_add(1, Ordering::SeqCst);
47+
})
48+
}
4249
.expect("succeeds");
4350
assert_eq!(V1.load(Ordering::Relaxed), 2, "nothing changed yet");
44-
let reg2 =
45-
gix::interrupt::init_handler(2, || V2.store(true, Ordering::SeqCst)).expect("multi-initialization is OK");
51+
// SAFETY: The closure doesn't use mutexes or memory allocation, so it should be safe to call from a signal handler.
52+
let reg2 = unsafe { gix::interrupt::init_handler(2, || V2.store(true, Ordering::SeqCst)) }
53+
.expect("multi-initialization is OK");
4654
assert!(!V2.load(Ordering::Relaxed));
4755

4856
signal_hook::low_level::raise(SIGTERM).expect("signal can be raised");
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
thread 'main' panicked at src/porcelain/main.rs:41:42:
1+
thread 'main' panicked at src/porcelain/main.rs:45:42:
22
something went very wrong
33
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
thread 'main' panicked at src/porcelain/main.rs:41:42:
1+
thread 'main' panicked at src/porcelain/main.rs:45:42:
22
something went very wrong
33
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
44

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[?1049h[?25lthread '<unnamed>' panicked at src/porcelain/main.rs:41:42:
1+
[?1049h[?25lthread '<unnamed>' panicked at src/porcelain/main.rs:45:42:
22
something went very wrong
33
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
44
[?25h[?1049l

0 commit comments

Comments
 (0)