Skip to content

Commit

Permalink
Make spawn_exec() asynchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-F-Bryan committed May 4, 2023
1 parent a8864aa commit 3ed1192
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 28 deletions.
17 changes: 9 additions & 8 deletions lib/wasi/src/bin_factory/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use crate::{
WasiFunctionEnv, WasiRuntime,
};

pub fn spawn_exec(
#[tracing::instrument(level = "trace", skip_all, fields(%name, %binary.package_name))]
pub async fn spawn_exec(
binary: BinaryPackage,
name: &str,
store: Store,
Expand All @@ -26,7 +27,9 @@ pub fn spawn_exec(
// The deterministic id for this engine
let compiler = store.engine().deterministic_id();

let module = compiled_modules.get_compiled_module(&**runtime, binary.hash().as_str(), compiler);
let module = compiled_modules
.get_compiled_module(&**runtime, binary.hash().as_str(), compiler)
.await;

let module = match (module, binary.entry.as_ref()) {
(Some(a), _) => a,
Expand All @@ -45,12 +48,9 @@ pub fn spawn_exec(
}
let module = module?;

compiled_modules.set_compiled_module(
&**runtime,
binary.hash().as_str(),
compiler,
&module,
);
compiled_modules
.set_compiled_module(&**runtime, binary.hash().as_str(), compiler, &module)
.await;
module
}
(None, None) => {
Expand Down Expand Up @@ -220,6 +220,7 @@ impl BinFactory {
&self.runtime,
&self.cache,
)
.await
})
}

Expand Down
10 changes: 4 additions & 6 deletions lib/wasi/src/bin_factory/module_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl ModuleCache {
})
}

pub fn get_compiled_module(
pub async fn get_compiled_module(
&self,
runtime: &dyn WasiRuntime,
data_hash: &str,
Expand All @@ -69,12 +69,10 @@ impl ModuleCache {
let engine = runtime.engine()?;

let tasks = runtime.task_manager();
tasks
.block_on(async { self.0.load(&key, &engine, tasks).await })
.ok()
self.0.load(&key, &engine, tasks).await.ok()
}

pub fn set_compiled_module(
pub async fn set_compiled_module(
&self,
runtime: &dyn WasiRuntime,
data_hash: &str,
Expand All @@ -84,7 +82,7 @@ impl ModuleCache {
let key = format!("{}-{}", data_hash, compiler);

let tasks = runtime.task_manager();
let result = tasks.block_on(async { self.0.save(&key, module, tasks).await });
let result = self.0.save(&key, module, tasks).await;

if let Err(e) = result {
tracing::warn!(
Expand Down
4 changes: 3 additions & 1 deletion lib/wasi/src/os/command/builtins/cmd_wasmer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ impl CmdWasmer {
// Get the binary
if let Some(binary) = self.get_package(what.clone()) {
// Now run the module
spawn_exec(binary, name, store, env, &self.runtime, &self.cache)
parent_ctx.data().tasks().block_on(async {
spawn_exec(binary, name, store, env, &self.runtime, &self.cache).await
})
} else {
parent_ctx.data().tasks().block_on(async move {
let _ = stderr_write(
Expand Down
4 changes: 2 additions & 2 deletions lib/wasi/src/os/console/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,14 @@ impl Console {

// Build the config
// Run the binary
let process = spawn_exec(
let process = tasks.block_on(spawn_exec(
binary,
prog,
store,
env,
&self.runtime,
self.compiled_modules.as_ref(),
)?;
))?;

// Return the process
Ok((process, wasi_process))
Expand Down
20 changes: 9 additions & 11 deletions lib/wasi/src/syscalls/wasix/proc_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,13 @@ pub fn proc_exec<M: MemorySize>(
let new_store = new_store.take().unwrap();
let env = config.take().unwrap();

tasks.block_on(async {
let name_inner = name.clone();
let ret = bin_factory.spawn(
name_inner,
new_store,
env,
)
.await;
let name_inner = name.clone();
__asyncify_light(ctx.data(), None, async {
let ret = bin_factory.spawn(name_inner, new_store, env).await;
match ret {
Ok(ret) => {
trace!(%child_pid, "spawned sub-process");
},
}
Err(err) => {
err_exit_code = conv_bus_err_to_exit_code(err);

Expand All @@ -127,10 +122,13 @@ pub fn proc_exec<M: MemorySize>(
&ctx,
format!("wasm execute failed [{}] - {}\n", name.as_str(), err)
.as_bytes(),
).await;
)
.await;
}
}
})

Ok(())
});
}
}
};
Expand Down

0 comments on commit 3ed1192

Please sign in to comment.