Skip to content

Commit d77bc0e

Browse files
committed
reduce size of unsafe block in signal handler
I guess it's generally good to keep unsafe blocks small, so it's easier for the reader to guess which part is unsafe.
1 parent ff1542c commit d77bc0e

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

gix/src/interrupt.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,21 +110,22 @@ mod init {
110110
// * we only set atomics or call functions that do
111111
// * there is no use of the heap
112112
let interrupt = interrupt.clone();
113+
let action = move || {
114+
static INTERRUPT_COUNT: AtomicUsize = AtomicUsize::new(0);
115+
if !super::is_triggered() {
116+
INTERRUPT_COUNT.store(0, Ordering::SeqCst);
117+
}
118+
let msg_idx = INTERRUPT_COUNT.fetch_add(1, Ordering::SeqCst);
119+
if msg_idx == grace_count {
120+
gix_tempfile::registry::cleanup_tempfiles_signal_safe();
121+
signal_hook::low_level::emulate_default_handler(*sig).ok();
122+
}
123+
interrupt();
124+
super::trigger();
125+
};
113126
#[allow(unsafe_code)]
114127
unsafe {
115-
let hook_id = signal_hook::low_level::register(*sig, move || {
116-
static INTERRUPT_COUNT: AtomicUsize = AtomicUsize::new(0);
117-
if !super::is_triggered() {
118-
INTERRUPT_COUNT.store(0, Ordering::SeqCst);
119-
}
120-
let msg_idx = INTERRUPT_COUNT.fetch_add(1, Ordering::SeqCst);
121-
if msg_idx == grace_count {
122-
gix_tempfile::registry::cleanup_tempfiles_signal_safe();
123-
signal_hook::low_level::emulate_default_handler(*sig).ok();
124-
}
125-
interrupt();
126-
super::trigger();
127-
})?;
128+
let hook_id = signal_hook::low_level::register(*sig, action)?;
128129
hooks.push((*sig, hook_id));
129130
}
130131
}

0 commit comments

Comments
 (0)