How to disable async in wasmtime::component?

105 views Asked by At

Test Case

WIT:

package component:adder;

/// An example world for the component to target.
world example {
    export add: func(x: s32, y: s32) -> s32;
}

WASM:

use anyhow::Context;
use std::path::PathBuf;
use wasmtime::component::*;
use wasmtime::{Config, Engine, Store};
use wasmtime_wasi::preview2::{self, command, Table, WasiCtx, WasiCtxBuilder, WasiView};

wasmtime::component::bindgen!({
    path: "adder.wit",
    world: "example",
    async: false,
});

pub fn add(x: i32, y: i32) -> wasmtime::Result<i32> {
    let mut config = Config::default();
    config.wasm_component_model(true);
    config.async_support(false);

    let engine = Engine::new(&config)?;
    let mut linker = Linker::new(&engine);

    // Add the command world (aka WASI CLI) to the linker
    command::add_to_linker(&mut linker).context("Failed to link command world")?;
    // environment::add_to_linker(&mut linker, |x| x)?;

    let wasi_view = ServerWasiView::new();
    let mut store = Store::new(&engine, wasi_view);

    let component =
        Component::from_file(&engine, "adder.wasm").context("Component file not found")?;

    let (instance, _) = Example::instantiate(&mut store, &component, &linker)
        .context("Failed to instantiate the example world")?;

    instance
        .call_add(&mut store, x, y)
        .context("Failed to call add function")
}

struct ServerWasiView {
    table: Table,
    ctx: WasiCtx,
}

impl ServerWasiView {
    fn new() -> Self {
        let table = Table::new();
        let ctx = WasiCtxBuilder::new().inherit_stdio().build();

        Self { table, ctx }
    }
}

impl WasiView for ServerWasiView {
    fn table(&self) -> &Table {
        &self.table
    }

    fn table_mut(&mut self) -> &mut Table {
        &mut self.table
    }

    fn ctx(&self) -> &WasiCtx {
        &self.ctx
    }

    fn ctx_mut(&mut self) -> &mut WasiCtx {
        &mut self.ctx
    }
}

#[tokio::main]
async fn main() -> wasmtime::Result<()> {
    let r = add(1, 2)?;
    println!("{}", r);
    Ok(())
}

Steps to Reproduce

  • build wasm cargo component build
  • build main/engine cargo build
  • run engine: ./target/debug/host-add

Expected Results

The wasm main/engine exectue as async example.

Actual Results

$ ./target/debug/host-add
thread 'main' panicked at /hpc/mtr_scrap/users/klausm/cargo_home/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-15.0.1/src/component/linker.rs:320:9:
cannot use `func_wrap_async` without enabling async support in the config
stack backtrace:
   0: rust_begin_unwind
             at /rustc/a28077b28a02b92985b3a3faecf92813155f1ea1/library/std/src/panicking.rs:597:5
   1: core::panicking::panic_fmt
             at /rustc/a28077b28a02b92985b3a3faecf92813155f1ea1/library/core/src/panicking.rs:72:14
   2: wasmtime::component::linker::LinkerInstance<T>::func_wrap_async
             at /hpc/mtr_scrap/users/klausm/cargo_home/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-15.0.1/src/component/linker.rs:320:9
   3: wasmtime_wasi::preview2::bindings::wasi::filesystem::types::add_to_linker
             at /hpc/mtr_scrap/users/klausm/cargo_home/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-wasi-15.0.1/src/preview2/mod.rs:94:5
   4: wasmtime_wasi::preview2::command::add_to_linker
             at /hpc/mtr_scrap/users/klausm/cargo_home/registry/src/index.crates.io-6f17d22bba15001f/wasmtime-wasi-15.0.1/src/preview2/command.rs:32:5
   5: host_add::add
             at ./src/main.rs:22:5
   6: host_add::main::{{closure}}
             at ./src/main.rs:73:13
   7: tokio::runtime::park::CachedParkThread::block_on::{{closure}}
             at /hpc/mtr_scrap/users/klausm/cargo_home/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.1/src/runtime/park.rs:282:63
   8: tokio::runtime::coop::with_budget
             at /hpc/mtr_scrap/users/klausm/cargo_home/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.1/src/runtime/coop.rs:107:5
   9: tokio::runtime::coop::budget
             at /hpc/mtr_scrap/users/klausm/cargo_home/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.1/src/runtime/coop.rs:73:5
  10: tokio::runtime::park::CachedParkThread::block_on
             at /hpc/mtr_scrap/users/klausm/cargo_home/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.1/src/runtime/park.rs:282:31
  11: tokio::runtime::context::blocking::BlockingRegionGuard::block_on
             at /hpc/mtr_scrap/users/klausm/cargo_home/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.1/src/runtime/context/blocking.rs:66:9
  12: tokio::runtime::scheduler::multi_thread::MultiThread::block_on::{{closure}}
             at /hpc/mtr_scrap/users/klausm/cargo_home/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.1/src/runtime/scheduler/multi_thread/mod.rs:87:13
  13: tokio::runtime::context::runtime::enter_runtime
             at /hpc/mtr_scrap/users/klausm/cargo_home/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.1/src/runtime/context/runtime.rs:65:16
  14: tokio::runtime::scheduler::multi_thread::MultiThread::block_on
             at /hpc/mtr_scrap/users/klausm/cargo_home/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.1/src/runtime/scheduler/multi_thread/mod.rs:86:9
  15: tokio::runtime::runtime::Runtime::block_on
             at /hpc/mtr_scrap/users/klausm/cargo_home/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.1/src/runtime/runtime.rs:350:45
  16: host_add::main
             at ./src/main.rs:75:5
  17: core::ops::function::FnOnce::call_once
             at /rustc/a28077b28a02b92985b3a3faecf92813155f1ea1/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Versions and Environment

Wasmtime version or commit: wasmtime: 15, rust/cargo: 1.74.1

Operating system: ubuntu 22.04

Architecture: x86

1

There are 1 answers

0
kmdreko On BEST ANSWER

It looks like command::add_to_linker is adding async functions to the linker. You should try using command::sync::add_to_linker instead.