Skip to content

Commit

Permalink
Fix handling of SIGTERM on *nix (#178)
Browse files Browse the repository at this point in the history
* Fix handling of SIGTERM on *nix

This is just for future extensibility

* Fix error codes

I have been silly enough to break error codes
  • Loading branch information
ohsayan committed Jun 22, 2021
1 parent 5be3041 commit d53a0cb
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ All changes in this project will be noted in this file.
- More than one process can no longer concurrently use the same data directory, preventing any possible data corruption [see [#169](https://github.com/skytable/skytable/pull/169), [#167](https://github.com/skytable/skytable/issues/167)]
- Fixed longstanding error in `sky-bench` component that caused key collisions
- Fixed bug in `sky-bench` that allowed passing 0 for the inputs
- Fixed handling of SIGTERM in `skyd` [see [#178](https://github.com/skytable/skytable/pull/178)]
- Fixed incorrect termination codes [see [#178](https://github.com/skytable/skytable/pull/178)]

### Additions

Expand Down
4 changes: 2 additions & 2 deletions cli/src/argparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub async fn start_repl() {
Ok(p) => p,
Err(_) => {
eprintln!("ERROR: Invalid port");
process::exit(0x100);
process::exit(0x01);
}
},
None => 2003,
Expand All @@ -63,7 +63,7 @@ pub async fn start_repl() {
Ok(c) => c,
Err(e) => {
eprintln!("ERROR: {}", e);
process::exit(0x100);
process::exit(0x01);
}
};
let mut runner = Runner::new(con);
Expand Down
4 changes: 2 additions & 2 deletions libstress/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ where
match self {
Self::Err(e) => {
log::error!("{} : '{}'", msg.to_string(), e);
std::process::exit(0x100);
std::process::exit(0x01);
}
Self::Ok(v) => v,
}
Expand All @@ -61,7 +61,7 @@ impl<T> ExitError<T> for Option<T> {
match self {
Self::None => {
log::error!("{}", msg.to_string());
std::process::exit(0x100);
std::process::exit(0x01);
}
Self::Some(v) => v,
}
Expand Down
2 changes: 1 addition & 1 deletion server/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ pub fn get_config_file_or_return_cfg() -> Result<ConfigType<ParsedConfig, String
if let Some(format) = matches.value_of("format") {
if let Err(e) = compat::upgrade(format) {
log::error!("Dataset upgrade failed with error: {}", e);
process::exit(0x100);
process::exit(0x01);
} else {
process::exit(0x000);
}
Expand Down
52 changes: 48 additions & 4 deletions server/src/dbnet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,34 @@ impl MultiListener {
}
}

#[cfg(unix)]
use core::{pin::Pin, task::Context, task::Poll};
#[cfg(unix)]
use tokio::signal::unix::{signal as fnsignal, Signal, SignalKind};
#[cfg(unix)]
/// Object to bind to unix-specific signals
pub struct UnixTerminationSignal {
sigterm: Signal,
}

#[cfg(unix)]
impl UnixTerminationSignal {
pub fn init() -> Result<Self, String> {
let sigterm = fnsignal(SignalKind::terminate())
.map_err(|e| format!("Failed to bind to signal with: {}", e))?;
Ok(Self { sigterm })
}
}

#[cfg(unix)]
impl Future for UnixTerminationSignal {
type Output = Option<()>;

fn poll(mut self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Self::Output> {
self.sigterm.poll_recv(ctx)
}
}

/// Start the server waiting for incoming connections or a CTRL+C signal
pub async fn run(
ports: PortConfig,
Expand Down Expand Up @@ -294,12 +322,28 @@ pub async fn run(
MultiListener::new_multi(secure_listener, insecure_listener, ssl).await?
}
};
tokio::select! {
_ = server.run_server() => {}
_ = sig => {
log::info!("Signalling all workers to shut down");
#[cfg(not(unix))]
{
// Non-unix, usually Windows specific signal handling.
// FIXME(@ohsayan): For now, let's just
// bother with ctrl+c, we'll move ahead as users require them
tokio::select! {
_ = server.run_server() => {}
_ = sig => {}
}
}
#[cfg(unix)]
{
let sigterm = UnixTerminationSignal::init()?;
// apart from CTRLC, the only other thing we care about is SIGTERM
// FIXME(@ohsayan): Maybe we should respond to SIGHUP too?
tokio::select! {
_ = server.run_server() => {},
_ = sig => {},
_ = sigterm => {}
}
}
log::info!("Signalling all workers to shut down");
// drop the signal and let others exit
drop(signal);
server.finish_with_termsig().await;
Expand Down
12 changes: 6 additions & 6 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn main() {
// uh oh, something happened while starting up
log::error!("{}", e);
pre_shutdown_cleanup(pid_file);
process::exit(0x100);
process::exit(1);
}
};
assert_eq!(
Expand Down Expand Up @@ -141,7 +141,7 @@ pub fn pre_shutdown_cleanup(pid_file: fs::File) {
drop(pid_file);
if let Err(e) = fs::remove_file(PATH) {
log::error!("Shutdown failure: Failed to remove pid file: {}", e);
process::exit(0x100);
process::exit(0x01);
}
}

Expand All @@ -168,7 +168,7 @@ fn check_args_and_get_cfg() -> (PortConfig, BGSave, SnapshotConfig, Option<Strin
}
Err(e) => {
log::error!("{}", e);
std::process::exit(0x100);
std::process::exit(0x01);
}
};
binding_and_cfg
Expand All @@ -190,7 +190,7 @@ fn run_pre_startup_tasks() -> fs::File {
"Startup failure: Another process with parent PID {} is using the data directory",
pid
);
process::exit(0x100);
process::exit(0x01);
}
let mut file = match fs::OpenOptions::new()
.create(true)
Expand All @@ -201,12 +201,12 @@ fn run_pre_startup_tasks() -> fs::File {
Ok(fle) => fle,
Err(e) => {
log::error!("Startup failure: Failed to open pid file: {}", e);
process::exit(0x100);
process::exit(0x01);
}
};
if let Err(e) = file.write_all(process::id().to_string().as_bytes()) {
log::error!("Startup failure: Failed to write to pid file: {}", e);
process::exit(0x100);
process::exit(0x01);
}
file
}
2 changes: 1 addition & 1 deletion sky-bench/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ macro_rules! sanity_test {
macro_rules! err {
($note:expr) => {{
eprintln!("ERROR: {}", $note);
std::process::exit(0x100);
std::process::exit(0x01);
}};
}

Expand Down

0 comments on commit d53a0cb

Please sign in to comment.