-
-
Notifications
You must be signed in to change notification settings - Fork 34
Implement graceful shutdown support #69
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,10 @@ use crate::cli::Cli; | |
| use clap::Parser; | ||
| use std::ffi::{CStr, c_char, c_int}; | ||
| use std::slice; | ||
| use std::sync::OnceLock; | ||
| use tokio_util::sync::CancellationToken; | ||
|
|
||
| static CANCEL_TOKEN: OnceLock<CancellationToken> = OnceLock::new(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really like static variables, but I think there aren't any way around it given we work with C library bindings?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I don't really know... |
||
|
|
||
| /// Some docs | ||
| /// | ||
|
|
@@ -42,6 +46,8 @@ pub unsafe extern "C" fn start_app(argc: c_int, argv: *const *const c_char) { | |
|
|
||
| match Cli::try_parse_from(&rust_args) { | ||
| Ok(cli) => { | ||
| let token = CANCEL_TOKEN.get_or_init(CancellationToken::new).clone(); | ||
|
|
||
| let rt = tokio::runtime::Builder::new_current_thread() | ||
| .enable_all() | ||
| .build() | ||
|
|
@@ -50,10 +56,23 @@ pub unsafe extern "C" fn start_app(argc: c_int, argv: *const *const c_char) { | |
| let _ = rt.block_on(server::start_server::start_server( | ||
| cli.config_path, | ||
| cli.verbose, | ||
| token, | ||
| )); | ||
| } | ||
| Err(e) => { | ||
| e.print().expect("Failed to print error"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Some docs | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// Pretty safe actually | ||
| #[unsafe(no_mangle)] | ||
| pub unsafe extern "C" fn stop_app() { | ||
| if let Some(token) = CANCEL_TOKEN.get() { | ||
| token.cancel(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,12 +8,17 @@ use crate::server::network::Server; | |
| use crate::server_state::{ServerState, ServerStateBuilderError}; | ||
| use std::path::PathBuf; | ||
| use std::process::ExitCode; | ||
| use tokio_util::sync::CancellationToken; | ||
| use tracing::{Level, debug, error}; | ||
| use tracing_subscriber::EnvFilter; | ||
| use tracing_subscriber::layer::SubscriberExt; | ||
| use tracing_subscriber::util::SubscriberInitExt; | ||
|
|
||
| pub async fn start_server(config_path: PathBuf, logging_level: u8) -> ExitCode { | ||
| pub async fn start_server( | ||
| config_path: PathBuf, | ||
| logging_level: u8, | ||
| token: CancellationToken, | ||
| ) -> ExitCode { | ||
| enable_logging(logging_level); | ||
| let Some(cfg) = load_configuration(&config_path) else { | ||
| return ExitCode::FAILURE; | ||
|
|
@@ -23,7 +28,12 @@ pub async fn start_server(config_path: PathBuf, logging_level: u8) -> ExitCode { | |
|
|
||
| match build_state(cfg) { | ||
| Ok(server_state) => { | ||
| Server::new(&bind, server_state).run().await; | ||
| tokio::select! { | ||
| () = Server::new(&bind, server_state).run() => {} | ||
| () = token.cancelled() => { | ||
| tracing::info!("Shutdown signal received, stopping server..."); | ||
| } | ||
| } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there is a similar logic for handling SIGINT and SIGTERM signals, have you looked at how those two logic (signals and explicit cancellation) can be merged and if it is even possible? |
||
| ExitCode::SUCCESS | ||
| } | ||
| Err(err) => { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are
volatileproperties and why are they needed here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
volatileensures cross-thread visibility, reads/write are done against main memoryPicoLimboRunner#run() and
PicoLimboRunner#stop() are called from different threads