-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RetryableFileReader
: account for user interrupts (#7801)
This makes sure `RetryableFileReader` polls for user interrupts where necessary, so a non-terminated RRD file never hangs the user's process for the rest of times. I tried various simpler approaches, but they all failed because... well, you know, UNIX signals. In the end I'm actually not too unhappy with this, really. * Fixes #7791
- Loading branch information
Showing
5 changed files
with
100 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use std::sync::atomic::AtomicBool; | ||
|
||
// --- | ||
|
||
static SIGINT_RECEIVED: AtomicBool = AtomicBool::new(false); | ||
|
||
/// Call this to start tracking `SIGINT`s. | ||
/// | ||
/// You can then call [`was_sigint_ever_caught`] at any point in time. | ||
#[cfg(not(any(target_os = "windows", target_arch = "wasm32")))] | ||
#[allow(unsafe_code)] | ||
#[allow(clippy::fn_to_numeric_cast_any)] | ||
pub fn track_sigint() { | ||
static ONCE: std::sync::Once = std::sync::Once::new(); | ||
|
||
ONCE.call_once(|| { | ||
// SAFETY: we're installing a signal handler. | ||
unsafe { | ||
libc::signal( | ||
libc::SIGINT, | ||
signal_handler as *const fn(libc::c_int) as libc::size_t, | ||
); | ||
} | ||
|
||
unsafe extern "C" fn signal_handler(signum: libc::c_int) { | ||
SIGINT_RECEIVED.store(true, std::sync::atomic::Ordering::Relaxed); | ||
|
||
// SAFETY: we're calling a signal handler. | ||
unsafe { | ||
libc::signal(signum, libc::SIG_DFL); | ||
libc::raise(signum); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
#[cfg(any(target_os = "windows", target_arch = "wasm32"))] | ||
#[allow(unsafe_code)] | ||
#[allow(clippy::fn_to_numeric_cast_any)] | ||
pub fn track_sigint() {} | ||
|
||
/// Returns whether a `SIGINT` was ever caught. | ||
/// | ||
/// Need to call [`track_sigint`] at least once first. | ||
pub fn was_sigint_ever_caught() -> bool { | ||
// If somebody forgot to call this, at least we will only miss the first SIGINT, but | ||
// SIGINT-spamming will still work. | ||
track_sigint(); | ||
|
||
SIGINT_RECEIVED.load(std::sync::atomic::Ordering::Relaxed) | ||
} |