You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在wasmtime::component中禁用异步?附报错排查

在Wasmtime Component中禁用异步支持触发Panic:cannot use func_wrap_async without enabling async support in the config

测试用例

WIT 定义

package component:adder;

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

宿主程序代码

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(())
}

复现步骤

  • 构建Wasm组件:cargo component build
  • 构建宿主程序:cargo build
  • 运行宿主程序:./target/debug/host-add

预期结果

宿主程序能够正常执行Wasm组件的add函数并输出计算结果。

实际结果

程序触发panic,报错信息如下:

$ ./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.

版本与环境

  • Wasmtime版本:15.0.1
  • Rust/Cargo版本:1.74.1
  • 操作系统:Ubuntu 22.04
  • 架构:x86_64

问题原因与解决方法

从栈回溯可以看到,panic触发点是command::add_to_linker调用——Wasmtime WASI Preview2的command模块内部依赖了异步函数包装(func_wrap_async),即使你的组件本身没有异步导出,只要链接该模块,就必须启用Wasmtime的异步支持。

有两种解决方式:

  1. 启用异步支持:如果无需严格禁用异步,将config.async_support(false)改为config.async_support(true),或直接删除该行(Wasmtime默认启用异步支持)。修改后的配置代码:
let mut config = Config::default();
config.wasm_component_model(true);
// 移除config.async_support(false),或改为true
  1. 移除不必要的WASI依赖:如果组件不需要WASI功能(比如这个加法组件仅做简单计算),直接删除command::add_to_linker相关代码,同时移除ServerWasiView定义和WASI相关导入。修改后的add函数:
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 linker = Linker::new(&engine); // 无需链接WASI

    let mut store = Store::new(&engine, ()); // 无需WASI上下文,使用空元组即可

    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")
}

修改后程序即可正常运行,无需启用异步支持。


内容的提问来源于stack exchange,提问作者Klaus Ma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 21:55:54