Skip to content

Commit

Permalink
Ctrl-C handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Oct 9, 2024
1 parent 1c5e6d8 commit f735bd5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ fn main() -> Result<(), BoxError> {
return Err("C API is not supported for server".into());
}

let join = ctrlc2::set_handler(|| {
let ctrlc_fired = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
let ctrlc_fired_clone = ctrlc_fired.clone();
let ctrl_handle = ctrlc2::set_handler(move || {
log::info!("Ctrl-C received, exiting...");
ctrlc_fired_clone.store(true, std::sync::atomic::Ordering::SeqCst);
unsafe { overtls::over_tls_client_stop() };
true
})?;
Expand All @@ -31,7 +34,9 @@ fn main() -> Result<(), BoxError> {

unsafe { overtls::over_tls_client_run(config_path, opt.verbosity, Some(port_cb), std::ptr::null_mut()) };

join.join().expect("Couldn't join on the associated thread");
if ctrlc_fired.load(std::sync::atomic::Ordering::SeqCst) {
ctrl_handle.join().map_err(|e| format!("{:?}", e))?;
}
} else if let Some(url) = opt.url_of_node.as_ref() {
let url_str = std::ffi::CString::new(url.as_str())?;
let url_ptr = url_str.as_ptr();
Expand All @@ -42,7 +47,9 @@ fn main() -> Result<(), BoxError> {

unsafe { overtls::over_tls_client_run_with_ssr_url(url_ptr, listen_addr, opt.verbosity, Some(port_cb), std::ptr::null_mut()) };

join.join().expect("Couldn't join on the associated thread");
if ctrlc_fired.load(std::sync::atomic::Ordering::SeqCst) {
ctrl_handle.join().map_err(|e| format!("{:?}", e))?;
}
} else {
return Err("Config file or node URL is required".into());
}
Expand Down
14 changes: 13 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,18 @@ pub(crate) fn b64str_to_address(s: &str, url_safe: bool) -> Result<Address> {

#[doc(hidden)]
pub async fn async_main(config: Config, allow_shutdown: bool, shutdown_token: CancellationToken) -> Result<()> {
let ctrlc_fired = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
let mut ctrlc_handle = None;
if allow_shutdown {
let shutdown_token_clone = shutdown_token.clone();
ctrlc2::set_async_handler(async move {
let ctrlc_fired_clone = ctrlc_fired.clone();
let handle = ctrlc2::set_async_handler(async move {
log::info!("Ctrl-C received, exiting...");
ctrlc_fired_clone.store(true, std::sync::atomic::Ordering::SeqCst);
shutdown_token_clone.cancel();
})
.await;
ctrlc_handle = Some(handle);
}

let main_body = async {
Expand All @@ -92,6 +97,13 @@ pub async fn async_main(config: Config, allow_shutdown: bool, shutdown_token: Ca
return Err("Config is not a client config".into());
}

if ctrlc_fired.load(std::sync::atomic::Ordering::SeqCst) {
let Some(handle) = ctrlc_handle else {
return Ok(());
};
log::info!("Waiting for Ctrl-C handler to finish...");
handle.await.map_err(|e| e.to_string())?;
}
Ok(())
};

Expand Down

0 comments on commit f735bd5

Please sign in to comment.