Skip to content

Backport 9217 to release-25.0.0#9259

Merged
alexcrichton merged 2 commits intobytecodealliance:release-25.0.0from
elliottt:trevor/backport-9210
Sep 16, 2024
Merged

Backport 9217 to release-25.0.0#9259
alexcrichton merged 2 commits intobytecodealliance:release-25.0.0from
elliottt:trevor/backport-9210

Conversation

@elliottt
Copy link
Copy Markdown
Member

Backport the fix for #9210 to the release-25.0.0 branch, and update the release notes to describe the fix.

swlynch99 and others added 2 commits September 16, 2024 12:05
…s across await points (bytecodealliance#9217)

* Use `tracing::Instrument` in generated bindings to avoid holding spans
across await points

Previously, if we had a WIT file with a function

async-fn: func();

then the generated code in `add_to_linker_get_host` would look like
this (if tracing is enabled and the function is async)

inst.func_wrap_async(
    "my-func",
    move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
        wasmtime::component::__internal::Box::new(async move {
            let span = tracing::span!(
                tracing::Level::TRACE,
                "wit-bindgen import",
                module = "test",
                function = "my-func",
            );
            let _enter = span.enter();
            tracing::event!(tracing::Level::TRACE, "call");
            let host = &mut host_getter(caller.data_mut());
            let r = Host::my_func(host).await;
            tracing::event!(
                tracing::Level::TRACE,
                result = tracing::field::debug(&r),
                "return"
            );
            Ok(r)
        })
    },
)?;

This keeps the tracing span active, even when the resulting future is
suspended. The end result is that other unrelated tokio tasks running on
the same thread in the meantime will be shown as executing within the
`wit-bindgen import` span.

This commit changes the codegen to instead generate

inst.func_wrap_async(
    "async-fn",
    move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
        use tracing::Instrument;

        let span = tracing::span!(
            tracing::Level::TRACE,
            "wit-bindgen import",
            module = "test",
            function = "async-fn",
        );
        wasmtime::component::__internal::Box::new(
            async move {
                tracing::event!(tracing::Level::TRACE, "call");
                let host = &mut host_getter(caller.data_mut());
                let r = Host::async_fn(host).await;
                tracing::event!(
                    tracing::Level::TRACE,
                    result = tracing::field::debug(&r),
                    "return"
                );
                Ok(r)
            }
            .instrument(span),
        )
    },
)?;

Here, `tracing::Instrument` takes care of entering the span when the
future is polled and exiting it when it is suspended.

Fixes bytecodealliance#9210

* Bless expanded macro outputs
@elliottt elliottt requested review from a team as code owners September 16, 2024 19:08
@elliottt elliottt requested review from alexcrichton and removed request for a team September 16, 2024 19:09
@alexcrichton alexcrichton enabled auto-merge (squash) September 16, 2024 19:11
@alexcrichton alexcrichton merged commit 75fa616 into bytecodealliance:release-25.0.0 Sep 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants