Skip to content

Commit

Permalink
Rewrite handling signals code without loop
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiltd committed Oct 31, 2024
1 parent 9f328f7 commit 2a748b4
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions crates/containerd-shim-wasmtime/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,36 +306,33 @@ where
log::debug!("loading wasm component");

wasmtime_wasi::runtime::in_tokio(async move {
let mut force_shutdown = false;
let exec = self.execute_component_async(ctx, component, func, stdio);

tokio::pin!(exec);

loop {
tokio::select! {
status = &mut exec => {
return status;
}
sig = wait_for_signal() => {
match sig? {
libc::SIGINT if !force_shutdown => {
// Request graceful shutdown; if successful, the loop will
// exit from the `exec` branch with a status code.
force_shutdown = true;
self.cancel.cancel();
}
sig => {
// On a second SIGINT or other signal, terminate the process
// without waiting for spawned tasks to finish.
return Ok(128 + sig);
}
}
}
tokio::select! {
status = self.execute_component_async(ctx, component, func, stdio) => {
status
}
status = self.handle_signals() => {
status
}
}
})
}

async fn handle_signals(&self) -> Result<i32> {
match wait_for_signal().await? {
libc::SIGINT => {
// Request graceful shutdown;
self.cancel.cancel();
}
sig => {
// On other signal, terminate the process without waiting for spawned tasks to finish.
return Ok(128 + sig);
}
}

// On a second SIGINT, terminate the process as well
wait_for_signal().await
}

fn execute(
&self,
ctx: &impl RuntimeContext,
Expand Down

0 comments on commit 2a748b4

Please sign in to comment.