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

add win interruption using tokio::windows for spk_monitor #1110

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
37 changes: 19 additions & 18 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/spfs-cli/cmd-monitor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ spfs-cli-common = { workspace = true }
tokio = { version = "1.20", features = ["rt", "rt-multi-thread"] }
tracing = { workspace = true }
url = "2.2"
futures = "0.3.30"
23 changes: 10 additions & 13 deletions crates/spfs-cli/cmd-monitor/src/cmd_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ use clap::Parser;
#[cfg(feature = "sentry")]
use cli::configure_sentry;
use miette::{Context, IntoDiagnostic, Result};
use spfs::Error;
use spfs_cli_common as cli;
use spfs_cli_common::CommandName;
use tokio::io::AsyncReadExt;
use tokio::signal::unix::{signal, SignalKind};
use tokio::time::timeout;

mod signal;
#[cfg(unix)]
use signal::unix_signal_handler::UnixSignalHandler as SignalHandlerImpl;
use signal::SignalHandler;
#[cfg(windows)]
use windows_signal_handler::WindowsSignalHandler as SignalHandlerImpl;

fn main() -> Result<()> {
// because this function exits right away it does not
// properly handle destruction of data, so we put the actual
Expand Down Expand Up @@ -86,11 +91,10 @@ impl CmdMonitor {
rt.block_on(self.wait_for_ready());
// clean up this runtime and all other threads before detaching
drop(rt);

#[cfg(unix)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this working for you? I think the monitor still needs to be background-ed to not block the runtime from launching, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you mean under windows?

So far i was able to trig the commands of course with error but not depending of the runtime blocking

nix::unistd::daemon(self.no_chdir, self.no_close)
.into_diagnostic()
.wrap_err("Failed to daemonize the monitor process")?;

#[cfg(feature = "sentry")]
{
// Initialize sentry after the call to `daemon` so it is safe for
Expand Down Expand Up @@ -142,12 +146,7 @@ impl CmdMonitor {
}

pub async fn run_async(&mut self, config: &spfs::Config) -> Result<i32> {
let mut interrupt = signal(SignalKind::interrupt())
.map_err(|err| Error::process_spawn_error("signal()", err, None))?;
let mut quit = signal(SignalKind::quit())
.map_err(|err| Error::process_spawn_error("signal()", err, None))?;
let mut terminate = signal(SignalKind::terminate())
.map_err(|err| Error::process_spawn_error("signal()", err, None))?;
let signal_future = SignalHandlerImpl::build_signal_future();

let repo = spfs::open_repository(&self.runtime_storage).await?;
let storage = spfs::runtime::Storage::new(repo)?;
Expand All @@ -165,9 +164,7 @@ impl CmdMonitor {
}
// we explicitly catch any signal related to interruption
// and will act by cleaning up the runtime early
_ = terminate.recv() => Err(spfs::Error::String("Terminate signal received, cleaning up runtime early".to_string())),
_ = interrupt.recv() => Err(spfs::Error::String("Interrupt signal received, cleaning up runtime early".to_string())),
_ = quit.recv() => Err(spfs::Error::String("Quit signal received, cleaning up runtime early".to_string())),
_ = signal_future => Err(spfs::Error::String("Signal received, cleaning up runtime early".to_string())),
};
tracing::trace!("runtime empty of processes ");

Expand Down
70 changes: 70 additions & 0 deletions crates/spfs-cli/cmd-monitor/src/signal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use std::pin::Pin;

use futures::future::Future;
use spfs::Error;

pub trait SignalHandler {
fn build_signal_future() -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>;
}

#[cfg(unix)]
pub mod unix_signal_handler {
use tokio::signal::unix::{signal, SignalKind};

use super::*;

pub struct UnixSignalHandler;

impl SignalHandler for UnixSignalHandler {
fn build_signal_future() -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>> {
Box::pin(async move {
let mut interrupt = signal(SignalKind::interrupt())
.map_err(|err| Error::process_spawn_error("signal()", err, None))?;
let mut quit = signal(SignalKind::quit())
.map_err(|err| Error::process_spawn_error("signal()", err, None))?;
let mut terminate = signal(SignalKind::terminate())
.map_err(|err| Error::process_spawn_error("signal()", err, None))?;

futures::future::select_all(vec![
Box::pin(interrupt.recv()),
Box::pin(quit.recv()),
Box::pin(terminate.recv()),
])
.await;

Ok(())
})
}
}
}

#[cfg(windows)]
pub mod windows_signal_handler {
use tokio::signal::ctrl_c;

use super::*;

pub struct WindowsSignalHandler;

impl SignalHandler for WindowsSignalHandler {
fn build_signal_future() -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>> {
Box::pin(async move {
let mut interrupt =
ctrl_c().map_err(|err| Error::process_spawn_error("ctrl_c()", err, None))?;
let mut quit =
ctrl_c().map_err(|err| Error::process_spawn_error("ctrl_c()", err, None))?;
let mut terminate =
ctrl_c().map_err(|err| Error::process_spawn_error("ctrl_c()", err, None))?;

futures::future::select_all(vec![
Box::pin(interrupt),
Box::pin(quit),
Box::pin(terminate),
])
.await;

Ok(())
})
}
}
}
Loading