-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix panic when dropping waitable set with stackful waiter #12971
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alexcrichton
merged 1 commit into
bytecodealliance:main
from
dicej:drop-waitable-set-with-stackful-waiters
Apr 6, 2026
+117
−4
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
tests/misc_testsuite/component-model/async/drop-waitable-set-stackful.wast
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| ;;! component_model_async = true | ||
| ;;! component_model_async_stackful = true | ||
| ;;! reference_types = true | ||
|
|
||
| ;; This is similar to the `drop-waitable-set.wast` test except that it uses | ||
| ;; "stackful" (i.e. no callback) async-lifted exports instead of "stackless" | ||
| ;; (i.e. with a callback) exports. That creates a situation where the waiter on | ||
| ;; the waitable set being dropped is a suspended fiber. Historically, there was | ||
| ;; a bug in Wasmtime such that we checked for waiters _after_ removing the set | ||
| ;; from the table, causing the fiber to be dropped and leading to a panic due to | ||
| ;; the fiber not having been disposed of gracefully. | ||
|
|
||
| (component | ||
| (component $C | ||
| (core module $Memory (memory (export "mem") 1)) | ||
| (core instance $memory (instantiate $Memory)) | ||
| (core module $Core | ||
| (import "" "mem" (memory 1)) | ||
| (import "" "waitable-set.new" (func $waitable-set.new (result i32))) | ||
| (import "" "waitable-set.wait" (func $waitable-set.wait (param i32 i32) (result i32))) | ||
| (import "" "waitable-set.drop" (func $waitable-set.drop (param i32))) | ||
|
|
||
| (global $ws (mut i32) (i32.const 0)) | ||
| (func $start (global.set $ws (call $waitable-set.new))) | ||
| (start $start) | ||
|
|
||
| ;; Stackful async: calls waitable-set.wait directly (blocks the fiber). | ||
| ;; The set is empty, so this will block indefinitely. | ||
| (func $wait-on-set (export "wait-on-set") | ||
| (drop (call $waitable-set.wait (global.get $ws) (i32.const 0))) | ||
| ) | ||
|
|
||
| ;; Attempts to drop the set while a fiber is waiting on it. | ||
| (func $drop-while-waiting (export "drop-while-waiting") | ||
| (call $waitable-set.drop (global.get $ws)) | ||
| unreachable | ||
| ) | ||
| ) | ||
| (canon waitable-set.new (core func $waitable-set.new)) | ||
| (canon waitable-set.wait (memory $memory "mem") (core func $waitable-set.wait)) | ||
| (canon waitable-set.drop (core func $waitable-set.drop)) | ||
| (core instance $core (instantiate $Core (with "" (instance | ||
| (export "mem" (memory $memory "mem")) | ||
| (export "waitable-set.new" (func $waitable-set.new)) | ||
| (export "waitable-set.wait" (func $waitable-set.wait)) | ||
| (export "waitable-set.drop" (func $waitable-set.drop)) | ||
| )))) | ||
|
|
||
| ;; KEY DIFFERENCE from callback test: `async` without `(callback ...)`. | ||
| ;; This makes the export use stackful fiber mode instead of callback mode. | ||
| ;; The core function runs on a fiber and can call blocking builtins directly. | ||
| (func (export "wait-on-set") async (canon lift | ||
| (core func $core "wait-on-set") | ||
| async | ||
| )) | ||
| (func (export "drop-while-waiting") async (canon lift | ||
| (core func $core "drop-while-waiting") | ||
| async | ||
| )) | ||
| ) | ||
|
|
||
| (component $D | ||
| (import "c" (instance $c | ||
| (export "wait-on-set" (func async)) | ||
| (export "drop-while-waiting" (func async)) | ||
| )) | ||
|
|
||
| (core module $Memory (memory (export "mem") 1)) | ||
| (core instance $memory (instantiate $Memory)) | ||
| (core module $Core | ||
| (import "" "mem" (memory 1)) | ||
| (import "" "wait-on-set" (func $wait-on-set (result i32))) | ||
| (import "" "drop-while-waiting" (func $drop-while-waiting)) | ||
| (func $run (export "run") (result i32) | ||
| (local $ret i32) | ||
|
|
||
| ;; Start an async call to wait-on-set. The callee's core function | ||
| ;; runs on a fiber and calls waitable-set.wait, which suspends it. | ||
| ;; The return value encodes (subtask_handle << 4) | status. | ||
| (local.set $ret (call $wait-on-set)) | ||
| (if (i32.ne (i32.const 1 (; STARTED ;)) (i32.and (local.get $ret) (i32.const 0xf))) | ||
| (then unreachable)) | ||
|
|
||
| ;; Now call drop-while-waiting, which tries to drop the waitable set | ||
| ;; that has a suspended fiber waiting on it. | ||
| (call $drop-while-waiting) | ||
| unreachable | ||
| ) | ||
| ) | ||
| (canon lower (func $c "wait-on-set") async (memory $memory "mem") (core func $wait-on-set')) | ||
| (canon lower (func $c "drop-while-waiting") (core func $drop-while-waiting')) | ||
| (core instance $core (instantiate $Core (with "" (instance | ||
| (export "mem" (memory $memory "mem")) | ||
| (export "wait-on-set" (func $wait-on-set')) | ||
| (export "drop-while-waiting" (func $drop-while-waiting')) | ||
| )))) | ||
| (func (export "run") async (result u32) (canon lift (core func $core "run"))) | ||
| ) | ||
|
|
||
| (instance $c (instantiate $C)) | ||
| (instance $d (instantiate $D (with "c" (instance $c)))) | ||
| (func (export "run") (alias export $d "run")) | ||
| ) | ||
|
|
||
| (assert_trap (invoke "run") "cannot drop waitable set with waiters") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be rewritten to not use
*_stackful = true?I think that should be fairly easy, just using a
callbackoption that only has anunreachableinstructionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't believe that's possible (or at least, I can't think of a way). The problem is that the test relies on being able to re-enter the instance while another task is already running on that instance, but that's not allowed if the already-running task is stackless (i.e. callback-based).
I just verified that by adding callbacks to component
$C's two exports and got a "deadlock detected: event loop cannot make further progress" trap instead of the desired "cannot drop waitable set with waiters" trap.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the record, I imagine we could do this by enabling
component_model_threadinginsteadcomponent_model_stackfuland spawning a thread in the task, but I don't think that's any better or more understandable than what we have now.