Skip to content

Commit

Permalink
Switch WasiRunner over to spawn_exec()
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-F-Bryan committed Oct 26, 2023
1 parent 853d49a commit fbd518b
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions lib/wasix/src/runners/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use crate::{
bin_factory::BinaryPackage,
capabilities::Capabilities,
runners::{wasi_common::CommonWasiOptions, MappedDirectory},
Runtime, WasiEnvBuilder,
runtime::task_manager::VirtualTaskManagerExt,
Runtime, WasiEnvBuilder, WasiRuntimeError,
};

#[derive(Debug, Default, Clone)]
Expand Down Expand Up @@ -228,25 +229,33 @@ impl crate::runners::Runner for WasiRunner {
.annotation("wasi")?
.unwrap_or_else(|| Wasi::new(command_name));

let module = runtime.load_module_sync(cmd.atom())?;
let mut store = runtime.new_store();
let store = runtime.new_store();

let env = self
.prepare_webc_env(command_name, &wasi, pkg, runtime, None)
.context("Unable to prepare the WASI environment")?;

if self
.wasi
.capabilities
.threading
.enable_asynchronous_threading
{
env.run_with_store_async(module, store)?;
.prepare_webc_env(command_name, &wasi, pkg, Arc::clone(&runtime), None)
.context("Unable to prepare the WASI environment")?
.build()?;

let command_name = command_name.to_string();
let tasks = runtime.task_manager().clone();
let pkg = pkg.clone();

let exit_code = tasks.spawn_and_block_on(async move {
let fut = crate::bin_factory::spawn_exec(pkg, &command_name, store, env, &runtime);
let mut task_handle = fut.await.context("Spawn failed")?;

task_handle
.wait_finished()
.await
.map_err(|err| Arc::into_inner(err).expect("Error shouldn't be shared"))
.context("Unable to wait for the process to exit")
})?;

if exit_code.raw() == 0 {
Ok(())
} else {
env.run_with_store(module, &mut store)?;
Err(WasiRuntimeError::Wasi(crate::WasiError::Exit(exit_code)).into())
}

Ok(())
}
}

Expand Down

0 comments on commit fbd518b

Please sign in to comment.