component::bindgen: Impossible to call async functions on host in imported async functions

Abierto
#15,424 3 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
12/100
Tipo de issue
Error
Claridad
Necesita aclaración
Estado de actividad
Activo
Stack tecnológico
rust, wasm

Línea de trabajo

The question centres on the bindgen! macro's imports block with async | trappable and the HostWithStore impl for HasSelf<PluginHost>, where evaluate_expression2 receives an &Accessor<T, Self>. Start with the Accessor::with and Accessor::spawn methods, and with the _7_async bindgen example the issue links to. Done would mean either a supported way for an async host function to await async host work, or a documented answer to that question. The issue is unresolved and needs a maintainer decision on the API first.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

wasm-proposal:component-model wasm-proposal:component-model-async wasmtime:api

component::bindgen! supports async for imported functions in two ways. First, in that it allows async implementations for any Wasm function. Second, in that it supports implementations of async Wasm functions.

I'm having a hard time with the latter because it requires the use of a WithStore interface, which in turn allows access to the host object via an Accessor. The Accessor deliberately only allows blocking access to the host, but I need async access to it. Ironically, perhaps, this is not a problem with blocking Wasm functions. But from what I can tell, it is simply impossible with async Wasm functions.

Here's an example.

From the WIT file:

...
interface host {
    evaluate-expression1: func(expression: string) -> result<option<string> string>;
    evaluate-expression2: async func(expression: string) -> result<option<string>, string>;
}

Generating the bindings:

wasmtime::component::bindgen!({
    ...
    imports: {
        "test:plugins/host.evaluate-expression1": async | trappable,
        "test:plugins/host.evaluate-expression2": async | trappable,
    }
});

Implementation:

impl imported::Host for PluginHost
{
    async fn evaluate_expression1(
        &mut self,
        expression: String,
    ) -> wasmtime::Result<Result<Option<String>, String>> {
        // No problem calling an async function on self here!
        Ok(self.evaluate_expression_impl(expression).await)
    }
}

impl imported::HostWithStore<T> for HasSelf<PluginHost>
{
    async fn evaluate_expression2(
        accessor: &Accessor<T, Self>,
        expression: imported::String,
    ) -> wasmtime::Result<Result<Option<String>, String>> {
        // How do I call evaluate_expression_impl here???
        todo!()
    }
}

I'm stuck. Accessor::with only supports blocking code and Accessor::spawn doesn't change that. The example code doesn't help, because it only shows blocking code. (I'm confused about this example. What is the use case for a blocking implementation of an async Wasm function?)

I think I understand the technical limitation. Imported async Wasm functions are being "driven" by the async implementation of the component, which the implementation does not control.

What can I do? Is this really impossible? My use case seems like it would be quite common. It seems to me that there should be a way to "hand over" my host async function to the component's async implementation to "drive". Or maybe I'm misunderstanding the situation entirely. :)

I would humbly suggest that if there is a solution, it should be shown in the example I linked above.

Lenguaje dominante
Rust
Estrellas
18.9k
Forks
2k
Merge medio
1 d 6 h
PR fusionados (30 d)
238

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de bytecodealliance/wasmtime

Todos los issues de bytecodealliance/wasmtime

Issues similares

Más issues de Rust

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.