Skip to content

add (off-by-default) task-group-hook feature - #14416

Merged
dicej merged 5 commits into
bytecodealliance:mainfrom
dicej:task-group-hook
Oct 1, 2026
Merged

dicej merged 5 commits into
bytecodealliance:mainfrom
dicej:task-group-hook

Conversation

@dicej

@dicej dicej commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

This addresses #14247 and roughly resembles the API sketch Alex posted as a comment on that issue.

Although the Component Model has no notion of a "task group", we define one here in order to enable embedders to associate guest->host calls with corresponding host->guest calls in a predictable way. See the doc comments on TaskGroupHook for details on what it means for a task to be part of a task group and what notifications a hook will receive for a given group.

Note that, unlike with CallHookHandler, the functions in TaskGroupHook do not have a StoreContextMut<T> parameter. Supporting such a parameter is technically possible, but would require converting a lot of functions so that they accept &mut dyn VMStore parameters instead of &mut StoreOpaque parameters, including parts of the public API. This is something we can revisit later, if desired. Meanwhile, types implementing TaskGroupHook may need to use e.g. Arc to share state with the T in Store<T>.

This feature supersedes StoreContextMut::async_call_stack, which I've removed given that it is prone to use-after-free of a table slot as described in the aforementioned issue.

Fixes #14247

@dicej
dicej requested a review from alexcrichton September 24, 2026 23:29
@dicej
dicej requested review from a team as code owners September 24, 2026 23:29
@github-actions github-actions Bot added the wasmtime:api Related to the API of the `wasmtime` crate itself label Sep 25, 2026
Comment thread crates/wasmtime/src/runtime/component/concurrent.rs Outdated
Comment thread crates/wasmtime/src/runtime/component/concurrent.rs Outdated
Comment thread crates/wasmtime/src/runtime/component/concurrent.rs Outdated

@alexcrichton alexcrichton left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this perhaps be something where a task_group_hook.rs submodule could be made to help split out code and reduce the #[cfg] traffic in concurrent.rs? (also in the spirit of keeping the file a bit smaller...)

Also, would it make sense to revert other work from #13510 such as the FuncCallConcurrent abstraction? (as that's sort of no longer relevant)

Comment thread crates/wasmtime/src/runtime/component/concurrent.rs Outdated
Comment thread crates/wasmtime/src/runtime/component/concurrent.rs Outdated
Comment thread crates/wasmtime/src/runtime/component/concurrent.rs Outdated
Comment thread crates/wasmtime/src/runtime/component/concurrent.rs Outdated
This addresses bytecodealliance#14247 and roughly resembles the API sketch Alex posted as a
comment on that issue.

Although the Component Model has no notion of a "task group", we define one here
in order to enable embedders to associate guest->host calls with corresponding
host->guest calls in a predictable way.  See the doc comments on `TaskGroupHook`
for details on what it means for a task to be part of a task group and what
notifications a hook will receive for a given group.

Note that, unlike with `CallHookHandler`, the functions in `TaskGroupHook` do
not have a `StoreContextMut<T>` parameter.  Supporting such a parameter is
technically possible, but would require converting a _lot_ of functions so that
they accept `&mut dyn VMStore` parameters instead of `&mut StoreOpaque`
parameters, including parts of the public API.  This is something we can revisit
later, if desired.  Meanwhile, types implementing `TaskGroupHook` may need to
use e.g. `Arc` to share state with the `T` in `Store<T>`.

This feature supersedes `StoreContextMut::async_call_stack`, which I've removed
given that it is prone to use-after-free of a table slot as described in the
aforementioned issue.

Fixes bytecodealliance#14247
@dicej
dicej requested a review from a team as a code owner September 29, 2026 15:08
@dicej
dicej requested review from pchickey and removed request for a team September 29, 2026 15:08
@dicej
dicej force-pushed the task-group-hook branch 2 times, most recently from 19a59a6 to 9e7aba7 Compare September 29, 2026 15:55
- Ensure `TaskGroupHook` events are delivered even if the guest traps and/or leaks subtasks
- Expand doc comments per review feedback
- Move most `TaskGroupHook`-related code into its own module
- Add `task_group_hook` function to `StoreContextMut`
- Deduplicate code and make it harder to forget to increment or decrement `TaskGroup` ref counts
@dicej

dicej commented Sep 29, 2026

Copy link
Copy Markdown
Contributor Author

I believe I've addressed all the feedback so far.

@alexcrichton alexcrichton left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few minor things, mostly I think a *_disabled.rs file would work well here, but otherwise lgtm

Comment thread crates/wasi-http/src/handler.rs Outdated
Comment thread crates/wasmtime/Cargo.toml
Comment thread crates/wasmtime/src/runtime/component/store.rs
state.debug_assert_deferred_host_invariant();
let old_thread = mem::replace(&mut state.unforced_current_thread, thread);

#[cfg(feature = "task-group-hook")]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW this file still has quite a lot of #[cfg] which IMO defeats most of the purpose of outlining it into task_group_hook.rs as the goal is to generally have everything in one location with little #[cfg] necessary. That being said I might recommend some changes here to assist with that and reduce the #[cfg] this file. Notably a task_group_hook_disabled.rs would be a location to have stubs that do nothing, and that would enable this file to ideally have far less #[cfg] to assume that task group management should always be done. If the feature is disabled the functions would be noops, but when enabled it'd route into task_group_hooks.rs.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify as well, I don't think that this file should have zero #[cfg] in it, but I think it could still be reduced quite a bit by having enabled/disabled files, and that I think is beneficial to have a file that contains 99%+ of the implementation as opposed to as-is where you've got to cross-reference a few file simultaneously to find the body of the implementation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just pushed an update; please let me know what you think.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Err, hold on, let me address the CI issues first.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, really this time.

@dicej
dicej force-pushed the task-group-hook branch 2 times, most recently from 629646c to 9bfa670 Compare September 29, 2026 22:18

@alexcrichton alexcrichton left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some minor nits, but lgtm 👍

Comment thread crates/wasmtime/src/runtime/component/concurrent_disabled.rs
Comment thread crates/wasmtime/src/runtime/component/store.rs Outdated
Comment on lines +112 to +114
#[cfg(feature = "task-group-hook")]
pub use task_group_hook::TaskGroupHook;
pub use task_group_hook::TaskGroupId;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be able to pub use task_group_hook::* to avoid individually listing each name

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but I tend to avoid wildcard imports; they make it harder for the reader to determine where a given symbol is defined.

Comment thread crates/wasmtime/src/runtime/component/concurrent.rs Outdated
Comment thread crates/wasmtime/src/runtime/component/concurrent.rs Outdated
ready_for_concurrent_call_waker: _,
event_loop_running: _,
#[cfg(feature = "task-group-hook")]
task_group_hook: _,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation a bit off

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, but rustfmt insists 🤷

@dicej
dicej enabled auto-merge September 30, 2026 22:47
@dicej
dicej added this pull request to the merge queue Sep 30, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Sep 30, 2026
@dicej
dicej enabled auto-merge October 1, 2026 00:34
@dicej
dicej added this pull request to the merge queue Oct 1, 2026
Merged via the queue into bytecodealliance:main with commit 0d76404 Oct 1, 2026
53 checks passed
@dicej
dicej deleted the task-group-hook branch October 1, 2026 01:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wasmtime:api Related to the API of the `wasmtime` crate itself

Projects

None yet

Development

Successfully merging this pull request may close these issues.

StoreContextMut::async_call_stack use-after-free of a table index slot, leading to bail-bugs

3 participants