Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ctrlc in place of signal hook on Windows #1123

Merged
merged 4 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading