Skip to content

Commit

Permalink
use async tasks to wait for container exit
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
  • Loading branch information
jprendes committed Feb 27, 2025
1 parent ae5cc22 commit 8ae2435
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
9 changes: 9 additions & 0 deletions crates/containerd-shim-wasm/src/sandbox/async_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::future::Future;
use std::sync::LazyLock;
use std::time::Duration;

use tokio::task::JoinHandle;
use tokio::time::timeout;

// A thread local runtime that can be used to run futures to completion.
Expand Down Expand Up @@ -31,6 +32,14 @@ pub trait AmbientRuntime: Future {
{
timeout(t, self).await.ok()
}

fn spawn(self) -> JoinHandle<Self::Output>
where
Self: Sized + Send + 'static,
Self::Output: Send + 'static,
{
RUNTIME.spawn(self)
}
}

impl<F: Future> AmbientRuntime for F {}
29 changes: 13 additions & 16 deletions crates/containerd-shim-wasm/src/sandbox/shim/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::thread;
#[cfg(feature = "opentelemetry")]
use std::time::Duration;

use anyhow::{Context as AnyhowContext, ensure};
use anyhow::ensure;
use containerd_shim::api::{
ConnectRequest, ConnectResponse, CreateTaskRequest, CreateTaskResponse, DeleteRequest, Empty,
KillRequest, ShutdownRequest, StartRequest, StartResponse, StateRequest, StateResponse,
Expand Down Expand Up @@ -252,21 +252,18 @@ impl<T: Instance + Send + Sync, E: EventSender> Local<T, E> {

let id = req.id().to_string();

thread::Builder::new()
.name(format!("{id}-wait"))
.spawn(move || {
let (exit_code, timestamp) = i.wait().block_on();
events.send(TaskExit {
container_id: id.clone(),
exit_status: exit_code,
exited_at: Some(timestamp.to_timestamp()).into(),
pid,
id,
..Default::default()
});
})
.context("could not spawn thread to wait exit")
.map_err(Error::from)?;
async move {
let (exit_code, timestamp) = i.wait().await;
events.send(TaskExit {
container_id: id.clone(),
exit_status: exit_code,
exited_at: Some(timestamp.to_timestamp()).into(),
pid,
id,
..Default::default()
});
}
.spawn();

debug!("started: {:?}", req);

Expand Down
10 changes: 5 additions & 5 deletions crates/containerd-shim-wasm/src/sys/unix/container/instance.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::marker::PhantomData;
use std::path::Path;
use std::thread;

use chrono::{DateTime, Utc};
use libcontainer::container::builder::ContainerBuilder;
Expand Down Expand Up @@ -101,11 +100,11 @@ impl<E: Engine + Default> SandboxInstance for Instance<E> {
self.container.start()?;

let exit_code = self.exit_code.clone();
thread::spawn(move || {
// move the exit code guard into this thread
async move {
// move the exit code guard into this task
let _guard = guard;

let status = match pidfd.wait().block_on() {
let status = match pidfd.wait().await {
Ok(WaitStatus::Exited(_, status)) => status,
Ok(WaitStatus::Signaled(_, sig, _)) => 128 + sig as i32,
Ok(res) => {
Expand All @@ -118,7 +117,8 @@ impl<E: Engine + Default> SandboxInstance for Instance<E> {
}
} as u32;
let _ = exit_code.set((status, Utc::now()));
});
}
.spawn();

Ok(pid as _)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/containerd-shim-wasm/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use oci_spec::runtime::{
get_default_namespaces,
};

use crate::sandbox::async_utils::AmbientRuntime;
use crate::sandbox::async_utils::AmbientRuntime as _;
use crate::sandbox::{Instance, InstanceConfig};

pub const TEST_NAMESPACE: &str = "runwasi-test";
Expand Down

0 comments on commit 8ae2435

Please sign in to comment.