Skip to content

Commit fcc881d

Browse files
committed
windows cancellation
1 parent 47e81ae commit fcc881d

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "dkn-compute"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
edition = "2021"
55
license = "Apache-2.0"
66
readme = "README.md"

src/main.rs

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1919
let cancellation_token = token.clone();
2020
// add cancellation check
2121
tokio::spawn(async move {
22+
// FIXME: weird feature-gating here bugs with IDE, fix this later
2223
#[cfg(feature = "profiling")]
2324
{
2425
const PROFILE_DURATION_SECS: u64 = 120;
@@ -86,22 +87,50 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
8687
Ok(())
8788
}
8889

89-
/// Waits for SIGTERM or SIGINT, and cancels the given token when the signal is received.
90+
// FIXME: remove this `unused` once we have a better way to handle this
91+
/// Waits for various termination signals, and cancels the given token when the signal is received.
92+
///
93+
/// Handles Unix and Windows [target families](https://doc.rust-lang.org/reference/conditional-compilation.html#target_family).
9094
#[allow(unused)]
9195
async fn wait_for_termination(cancellation: CancellationToken) -> std::io::Result<()> {
92-
use tokio::signal::unix::{signal, SignalKind};
96+
#[cfg(unix)]
97+
{
98+
use tokio::signal::unix::{signal, SignalKind};
99+
let mut sigterm = signal(SignalKind::terminate())?; // Docker sends SIGTERM
100+
let mut sigint = signal(SignalKind::interrupt())?; // Ctrl+C sends SIGINT
101+
tokio::select! {
102+
_ = sigterm.recv() => log::warn!("Recieved SIGTERM"),
103+
_ = sigint.recv() => log::warn!("Recieved SIGINT"),
104+
_ = cancellation.cancelled() => {
105+
// no need to wait if cancelled anyways
106+
// although this is not likely to happen
107+
return Ok(());
108+
}
109+
};
110+
}
93111

94-
let mut sigterm = signal(SignalKind::terminate())?; // Docker sends SIGTERM
95-
let mut sigint = signal(SignalKind::interrupt())?; // Ctrl+C sends SIGINT
96-
tokio::select! {
97-
_ = sigterm.recv() => log::warn!("Recieved SIGTERM"),
98-
_ = sigint.recv() => log::warn!("Recieved SIGINT"),
99-
_ = cancellation.cancelled() => {
100-
// no need to wait if cancelled anyways
101-
// although this is not likely to happen
102-
return Ok(());
103-
}
104-
};
112+
#[cfg(windows)]
113+
{
114+
use tokio::signal::windows;
115+
116+
// https://learn.microsoft.com/en-us/windows/console/handlerroutine
117+
let mut signal_c = windows::ctrl_c()?;
118+
let mut signal_break = windows::ctrl_break()?;
119+
let mut signal_close = windows::ctrl_close()?;
120+
let mut signal_shutdown = windows::ctrl_shutdown()?;
121+
122+
tokio::select! {
123+
_ = signal_c.recv() => log::warn!("Received CTRL_C"),
124+
_ = signal_break.recv() => log::warn!("Received CTRL_BREAK"),
125+
_ = signal_close.recv() => log::warn!("Received CTRL_CLOSE"),
126+
_ = signal_shutdown.recv() => log::warn!("Received CTRL_SHUTDOWN"),
127+
_ = cancellation.cancelled() => {
128+
// no need to wait if cancelled anyways
129+
// although this is not likely to happen
130+
return Ok(());
131+
}
132+
};
133+
}
105134

106135
log::info!("Terminating the node...");
107136
cancellation.cancel();

0 commit comments

Comments
 (0)