Skip to content

Commit 11e65d6

Browse files
authored
Only initialize signal handler once (#2953)
1 parent 1b32e27 commit 11e65d6

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/run.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,17 @@ pub fn run(args: impl Iterator<Item = impl Into<OsString> + Clone>) -> Result<()
3434
error.code().unwrap_or(EXIT_FAILURE)
3535
})
3636
}
37+
38+
#[cfg(test)]
39+
mod tests {
40+
use super::*;
41+
42+
#[test]
43+
fn run_can_be_called_more_than_once() {
44+
let tmp = testing::tempdir();
45+
fs::write(tmp.path().join("justfile"), "foo:").unwrap();
46+
let search_directory = format!("{}/", tmp.path().to_str().unwrap());
47+
run(["just", &search_directory].iter()).unwrap();
48+
run(["just", &search_directory].iter()).unwrap();
49+
}
50+
}

src/signal_handler.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ use super::*;
33
pub(crate) struct SignalHandler {
44
caught: Option<Signal>,
55
children: BTreeMap<i32, Command>,
6+
initialized: bool,
67
verbosity: Verbosity,
78
}
89

910
impl SignalHandler {
1011
pub(crate) fn install(verbosity: Verbosity) -> RunResult<'static> {
1112
let mut instance = Self::instance();
1213
instance.verbosity = verbosity;
13-
Platform::install_signal_handler(|signal| Self::instance().interrupt(signal))
14+
if !instance.initialized {
15+
Platform::install_signal_handler(|signal| Self::instance().handle(signal))?;
16+
instance.initialized = true;
17+
}
18+
Ok(())
1419
}
1520

1621
pub(crate) fn instance() -> MutexGuard<'static, Self> {
@@ -35,11 +40,12 @@ impl SignalHandler {
3540
Self {
3641
caught: None,
3742
children: BTreeMap::new(),
43+
initialized: false,
3844
verbosity: Verbosity::default(),
3945
}
4046
}
4147

42-
fn interrupt(&mut self, signal: Signal) {
48+
fn handle(&mut self, signal: Signal) {
4349
if signal.is_fatal() {
4450
if self.children.is_empty() {
4551
process::exit(signal.code());

0 commit comments

Comments
 (0)