Skip to content

Commit

Permalink
make methods in Local async
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 28, 2025
1 parent 28b9e9a commit abf630a
Show file tree
Hide file tree
Showing 10 changed files with 326 additions and 245 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/containerd-shim-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ sha256 = { workspace = true }
serde_bytes = "0.11"
prost = "0.13"
toml = "0.8"
trait-variant = "0.1"
tokio-async-drop = "0.1"

# tracing
# note: it's important to keep the version of tracing in sync with tracing-subscriber
Expand Down
11 changes: 6 additions & 5 deletions crates/containerd-shim-wasm/src/sandbox/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,29 @@ pub struct InstanceConfig {
/// Instance is a trait that gets implemented by consumers of this library.
/// This trait requires that any type implementing it is `'static`, similar to `std::any::Any`.
/// This means that the type cannot contain a non-`'static` reference.
#[trait_variant::make(Send)]
pub trait Instance: 'static {
/// The WASI engine type
type Engine: Send + Sync + Clone;

/// Create a new instance
fn new(id: String, cfg: &InstanceConfig) -> Result<Self, Error>
async fn new(id: String, cfg: &InstanceConfig) -> Result<Self, Error>
where
Self: Sized;

/// Start the instance
/// The returned value should be a unique ID (such as a PID) for the instance.
/// Nothing internally should be using this ID, but it is returned to containerd where a user may want to use it.
fn start(&self) -> Result<u32, Error>;
async fn start(&self) -> Result<u32, Error>;

/// Send a signal to the instance
fn kill(&self, signal: u32) -> Result<(), Error>;
async fn kill(&self, signal: u32) -> Result<(), Error>;

/// Delete any reference to the instance
/// This is called after the instance has exited.
fn delete(&self) -> Result<(), Error>;
async fn delete(&self) -> Result<(), Error>;

/// Waits for the instance to finish and returns its exit code
/// This is an async call.
fn wait(&self) -> impl Future<Output = (u32, DateTime<Utc>)> + Send;
async fn wait(&self) -> (u32, DateTime<Utc>);
}
11 changes: 6 additions & 5 deletions crates/containerd-shim-wasm/src/sandbox/shim/cli.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
use std::env::current_dir;
use std::fmt::Debug;
use std::sync::Arc;

use chrono::Utc;
use containerd_shim::error::Error as ShimError;
use containerd_shim::publisher::RemotePublisher;
use containerd_shim::util::write_address;
use containerd_shim::{self as shim, ExitSignal, api};
use containerd_shim::{self as shim, api};
use oci_spec::runtime::Spec;
use shim::Flags;

use crate::sandbox::async_utils::AmbientRuntime as _;
use crate::sandbox::instance::Instance;
use crate::sandbox::shim::events::{RemoteEventSender, ToTimestamp};
use crate::sandbox::shim::local::Local;
use crate::sandbox::sync::WaitableCell;

/// Cli implements the containerd-shim cli interface using `Local<T>` as the task service.
pub struct Cli<T: Instance + Sync + Send> {
engine: T::Engine,
namespace: String,
containerd_address: String,
exit: Arc<ExitSignal>,
exit: WaitableCell<()>,
_id: String,
}

Expand Down Expand Up @@ -50,7 +51,7 @@ where
engine: Default::default(),
namespace: args.namespace.to_string(),
containerd_address: args.address.clone(),
exit: Arc::default(),
exit: WaitableCell::new(),
_id: args.id.to_string(),
}
}
Expand Down Expand Up @@ -78,7 +79,7 @@ where

#[cfg_attr(feature = "tracing", tracing::instrument(level = "Info"))]
fn wait(&mut self) {
self.exit.wait();
self.exit.wait().block_on();
}

#[cfg_attr(
Expand Down
19 changes: 11 additions & 8 deletions crates/containerd-shim-wasm/src/sandbox/shim/instance_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ pub(super) struct InstanceData<T: Instance> {

impl<T: Instance> InstanceData<T> {
#[cfg_attr(feature = "tracing", tracing::instrument(level = "Debug"))]
pub fn new(id: impl AsRef<str> + std::fmt::Debug, config: InstanceConfig) -> Result<Self> {
pub async fn new(
id: impl AsRef<str> + std::fmt::Debug,
config: InstanceConfig,
) -> Result<Self> {
let id = id.as_ref().to_string();
let instance = T::new(id, &config)?;
let instance = T::new(id, &config).await?;
Ok(Self {
instance,
config,
Expand All @@ -31,11 +34,11 @@ impl<T: Instance> InstanceData<T> {
}

#[cfg_attr(feature = "tracing", tracing::instrument(skip(self), level = "Debug"))]
pub fn start(&self) -> Result<u32> {
pub async fn start(&self) -> Result<u32> {
let mut s = self.state.write().unwrap();

Check failure on line 38 in crates/containerd-shim-wasm/src/sandbox/shim/instance_data.rs

View workflow job for this annotation

GitHub Actions / wamr / lint on ubuntu-latest

this `MutexGuard` is held across an await point

Check failure on line 38 in crates/containerd-shim-wasm/src/sandbox/shim/instance_data.rs

View workflow job for this annotation

GitHub Actions / wamr / lint on ubuntu-latest

this write lock is used only for reading
s.start()?;

let res = self.instance.start();
let res = self.instance.start().await;

// These state transitions are always `Ok(())` because
// we hold the lock since `s.start()`
Expand All @@ -51,19 +54,19 @@ impl<T: Instance> InstanceData<T> {
}

#[cfg_attr(feature = "tracing", tracing::instrument(skip(self), level = "Debug"))]
pub fn kill(&self, signal: u32) -> Result<()> {
pub async fn kill(&self, signal: u32) -> Result<()> {
let mut s = self.state.write().unwrap();

Check failure on line 58 in crates/containerd-shim-wasm/src/sandbox/shim/instance_data.rs

View workflow job for this annotation

GitHub Actions / wamr / lint on ubuntu-latest

this `MutexGuard` is held across an await point

Check failure on line 58 in crates/containerd-shim-wasm/src/sandbox/shim/instance_data.rs

View workflow job for this annotation

GitHub Actions / wamr / lint on ubuntu-latest

this write lock is used only for reading
s.kill()?;

self.instance.kill(signal)
self.instance.kill(signal).await
}

#[cfg_attr(feature = "tracing", tracing::instrument(skip(self), level = "Debug"))]
pub fn delete(&self) -> Result<()> {
pub async fn delete(&self) -> Result<()> {
let mut s = self.state.write().unwrap();

Check failure on line 66 in crates/containerd-shim-wasm/src/sandbox/shim/instance_data.rs

View workflow job for this annotation

GitHub Actions / wamr / lint on ubuntu-latest

this `MutexGuard` is held across an await point

Check failure on line 66 in crates/containerd-shim-wasm/src/sandbox/shim/instance_data.rs

View workflow job for this annotation

GitHub Actions / wamr / lint on ubuntu-latest

this write lock is used only for reading
s.delete()?;

let res = self.instance.delete();
let res = self.instance.delete().await;

if res.is_err() {
// Always `Ok(())` because we hold the lock since `s.delete()`
Expand Down
Loading

0 comments on commit abf630a

Please sign in to comment.