Skip to content

Commit

Permalink
Implement ctrlc in place of signal-hook on Windows (#1123)
Browse files Browse the repository at this point in the history
* Implement ctrlc for Windows

* Add Windows CI workflow

* Fix unused import on Windows by conditionally importing spawn

* Pin ctrlc to =3.4.2 for Debian stable compatibility
  • Loading branch information
seriouscatsoserious authored Dec 16, 2024
1 parent c4086ca commit 3e4b498
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ jobs:
build:
name: Build

runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ubuntu-latest, windows-latest]
build-args:
[
--locked --no-default-features,
--locked,
--locked --features metrics_process,
--locked
]
include:
- os: ubuntu-latest
build-args: --locked --features metrics_process

steps:
- uses: actions/checkout@v3
Expand Down
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ rayon = "1.9"
serde = "1.0"
serde_derive = "1.0, <=1.0.171" # avoid precompiled binaries (https://github.com/serde-rs/serde/issues/2538)
serde_json = "1.0"
signal-hook = "0.3"
tiny_http = { version = "0.12", optional = true }

[target.'cfg(windows)'.dependencies]
ctrlc = "=3.4.2"

[target.'cfg(not(windows))'.dependencies]
signal-hook = "0.3"

[dependencies.electrs-rocksdb]
version = "0.19.0-e3"

Expand Down
26 changes: 26 additions & 0 deletions src/signals.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#[cfg(not(windows))]
use anyhow::Context;
use crossbeam_channel::{unbounded, Receiver};
#[cfg(not(windows))]
use signal_hook::consts::signal::*;
#[cfg(not(windows))]
use signal_hook::iterator::Signals;

use std::sync::{
Expand All @@ -9,6 +12,7 @@ use std::sync::{
};
use std::{error, fmt};

#[cfg(not(windows))]
use crate::thread::spawn;

#[derive(Debug)]
Expand Down Expand Up @@ -53,6 +57,7 @@ pub(crate) struct Signal {
}

impl Signal {
#[cfg(not(windows))]
pub fn new() -> Signal {
let ids = vec![
SIGINT, SIGTERM,
Expand Down Expand Up @@ -80,6 +85,27 @@ impl Signal {
result
}

#[cfg(windows)]
pub fn new() -> Signal {
let (tx, rx) = unbounded();
let result = Signal {
rx,
exit: ExitFlag::new(),
};

let exit_flag = result.exit.clone();

// Handle Ctrl-C
ctrlc::set_handler(move || {
info!("notified via Ctrl-C");
exit_flag.set();
let _ = tx.send(());
})
.expect("failed to set Ctrl-C handler");

result
}

pub fn receiver(&self) -> &Receiver<()> {
&self.rx
}
Expand Down

0 comments on commit 3e4b498

Please sign in to comment.