Stream: git-wasmtime

Topic: wasmtime / PR #15336 fix CM async spec mismatch regarding...


view this post on Zulip Wasmtime GitHub notifications bot (Nov 17 2026 at 22:23):

dicej requested alexcrichton for a review on PR #15336.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 17 2026 at 22:23):

dicej opened PR #15336 from dicej:fix-barging-ahead-of-pending-tasks to bytecodealliance:main:

The spec says that new tasks are not allowed to "barge ahead" of earlier calls which have been queued due to backpressure, i.e. they need to wait until the queued calls have started before they can run.

The solution here is to maintain a count of those calls (ConcurrentInstanceState::pending_start_count) and add a was_pending field to GuestCallKind::StartImplicit. When creating a new call, we set was_pending to false. Then, if backpressure requires us to queue the call, we set was_pending to true and increment pending_start_count. GuestCall::is_ready now checks both was_pending and pending_start_count (in addition to backpressure, etc.) to determine whether a task can start or needs to be queued, per the spec. Finally, any time we start or cancel a task with was_pending set to true, we decrement pending_start_count.

Claude found the bug and wrote the test case. I wrote the fix independently. I take responsibility for all of it.

<!--
Please make sure you include the following information:

Our development process is documented in the Wasmtime book:
https://docs.wasmtime.dev/contributing-development-process.html

Please review the Bytecode Alliance's AI tool usage policy at
https://github.com/bytecodealliance/governance/blob/main/AI_TOOL_POLICY.md

Please ensure all communication follows the code of conduct:
https://github.com/bytecodealliance/wasmtime/blob/main/CODE_OF_CONDUCT.md
-->

view this post on Zulip Wasmtime GitHub notifications bot (Nov 17 2026 at 22:23):

dicej requested wasmtime-core-reviewers for a review on PR #15336.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 00:52):

github-actions[bot] added the label wasmtime:api on PR #15336.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 14:25):

dicej converted PR #15336 fix CM async spec mismatch regarding backpressure-pending calls to a draft.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 14:27):

dicej commented on PR #15336:

Switching this to draft temporarily because I realized last night that neither the runtime code nor the test cover all the cases where we need to call StoreOpaque::partition_pending. I'll address that today and switch it back to ready-for-review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 15:02):

dicej commented on PR #15336:

Switching this to draft temporarily because I realized last night that neither the runtime code nor the test cover all the cases where we need to call StoreOpaque::partition_pending. I'll address that today and switch it back to ready-for-review.

Nevermind, false alarm. I had thought last night that we needed to call StoreOpaque::partition_pending any time ConcurrentInstanceState::pending_start_count went to zero, similar to how we call it when ConcurrentInstanceState::backpressure goes to zero, but after taking a closer look I remembered that the former will only go to zero _after_ the relevant items have already been removed from ConcurrentInstanceState::pending, so there will be nothing left to remove. Also, the barge-past-waiting-to-enter.wast test is covering the case I thought it was missing by waiting for all subtasks to complete (or be cancelled) before exiting.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 15:02):

dicej has marked PR #15336 as ready for review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 16:09):

:memo: alexcrichton submitted PR review:

One question is that I don't believe this currently handles cancellation, for example this test fails:

<details>

(component
  (component $C
    (core func $backpressure.inc (canon backpressure.inc))
    (core func $backpressure.dec (canon backpressure.dec))
    (func (export "bp-on") (canon lift (core func $backpressure.inc)))
    (func (export "bp-off") (canon lift (core func $backpressure.dec)))

    (core func $task.return (canon task.return))
    (core module $m
      (import "" "task.return" (func $task.return))
      (func (export "f") (result i32)
        (call $task.return)
        (i32.const 0)) ;; CALLBACK_CODE_EXIT
      (func (export "f-cb") (param i32 i32 i32) (result i32) unreachable)
    )
    (core instance $i (instantiate $m
      (with "" (instance
        (export "task.return" (func $task.return))
      ))
    ))
    (func (export "f") async
      (canon lift (core func $i "f") async (callback (core func $i "f-cb"))))
  )
  (instance $c (instantiate $C))

  (component $A
    (import "c" (instance $c
      (export "bp-on" (func))
      (export "bp-off" (func))
      (export "f" (func async))
    ))
    (core module $libc (memory (export "mem") 1))
    (core instance $libc (instantiate $libc))
    (core func $bp-on (canon lower (func $c "bp-on")))
    (core func $bp-off (canon lower (func $c "bp-off")))
    (core func $f (canon lower (func $c "f") async (memory (core memory $libc "mem"))))
    (core func $subtask.cancel (canon subtask.cancel))
    (core func $subtask.drop (canon subtask.drop))
    (core func $waitable-set.new (canon waitable-set.new))
    (core func $waitable.join (canon waitable.join))
    (core func $task.return (canon task.return))
    (core module $m
      (import "" "bp-on" (func $bp-on))
      (import "" "bp-off" (func $bp-off))
      (import "" "f" (func $f (result i32)))
      (import "" "subtask.cancel" (func $subtask.cancel (param i32) (result i32)))
      (import "" "subtask.drop" (func $subtask.drop (param i32)))
      (import "" "waitable-set.new" (func $waitable-set.new (result i32)))
      (import "" "waitable.join" (func $waitable.join (param i32 i32)))
      (import "" "task.return" (func $task.return))
      (global $set (mut i32) (i32.const 0))
      (global $pending (mut i32) (i32.const 0))

      ;; Calls `f`, asserts that the subtask is STARTING, and returns its
      ;; handle.
      (func $start-f (result i32)
        (local $st i32)
        (local.set $st (call $f))
        ;; low 4 bits are the subtask status; 0 = STARTING
        (if (i32.ne (i32.and (local.get $st) (i32.const 0xf)) (i32.const 0))
          (then unreachable))
        ;; the subtask handle is in the bits above the 4-bit status
        (i32.shr_u (local.get $st) (i32.const 4)))

      ;; Cancels the not-yet-started subtask `$h` and drops it.
      (func $cancel (param $h i32)
        ;; 3 = CANCELLED_BEFORE_STARTED
        (if (i32.ne (call $subtask.cancel (local.get $h)) (i32.const 3))
          (then unreachable))
        (call $subtask.drop (local.get $h)))

      (func (export "run") (result i32)
        (local $h1 i32)
        (global.set $set (call $waitable-set.new))
        (call $bp-on)
        ;; first call is queued behind backpressure => STARTING
        (local.set $h1 (call $start-f))
        (call $bp-off)
        ;; second call: queued behind h1 => STARTING
        (call $waitable.join (call $start-f) (global.get $set))
        ;; cancel the call ahead of it; the second call must still run
        (call $cancel (local.get $h1))
        (global.set $pending (i32.const 1))
        ;; CALLBACK_CODE_WAIT (2) on `$set`, which goes in the bits above 4
        (i32.or (i32.shl (global.get $set) (i32.const 4)) (i32.const 2)))

      (func (export "cb") (param $ev i32) (param $h i32) (param $status i32) (result i32)
        ;; 1 = EVENT_SUBTASK
        (if (i32.ne (local.get $ev) (i32.const 1)) (then unreachable))
        ;; 2 = RETURNED
        (if (i32.eq (local.get $status) (i32.const 2))
          (then
            (call $subtask.drop (local.get $h))
            (global.set $pending (i32.sub (global.get $pending) (i32.const 1)))))
        (if (i32.eqz (global.get $pending))
          (then
            (call $task.return)
            (return (i32.const 0)))) ;; CALLBACK_CODE_EXIT
        ;; CALLBACK_CODE_WAIT (2) on `$set`, which goes in the bits above 4
        (i32.or (i32.shl (global.get $set) (i32.const 4)) (i32.const 2)))
    )
    (core instance $i (instantiate $m
      (with "" (instance
        (export "bp-on" (func $bp-on))
        (export "bp-off" (func $bp-off))
        (export "f" (func $f))
        (export "subtask.cancel" (func $subtask.cancel))
        (export "subtask.drop" (func $subtask.drop))
        (export "waitable-set.new" (func $waitable-set.new))
        (export "waitable.join" (func $waitable.join))
        (export "task.return" (func $task.return))
      ))
    ))
    (func (export "run") async
      (canon lift (core func $i "run") async (callback (core func $i "cb"))))
  )
  (instance $a (instantiate $A (with "c" (instance $c))))
  (export "run" (func $a "run"))
)

(assert_return (invoke "run"))

</details>

Specifically if two calls are queued, the first is cancelled, then the second doesn't get to proceed as expected.


A second question, the original bug report this is coming from additionally mentioned that Accessor::poll_ready_for_concurrent_call doesn't take any of this new logic into account, erroneously saying that an instance is ready for a call when it in fact gets immediately queued. Did you want to fix that here, or in a separate PR?

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 16:09):

:speech_balloon: alexcrichton created PR review comment:

I'm confused by this comment and the growing boolean expression here -- the is_ready function here is making a deduction about self, this comment is referring to other calls, but only self is considered? How is the state of other calls factored in here?

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 16:41):

:memo: dicej submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 16:41):

:speech_balloon: dicej created PR review comment:

The state.pending_start_count == 0 clause means "there are no calls waiting in line", and the complete expression (*was_pending || state.pending_start_count == 0) means "if I am waiting in line (or was previously waiting in line before I got the go-ahead), then I have the right to go ahead regardless of who else is also in line; otherwise, if there _is_ anyone in line, I need to get in line too if I want a turn".

Does that help? If so, I can expand the comment.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 16:42):

dicej commented on PR #15336:

Did you want to fix that here, or in a separate PR?

I'm working on a separate issue right now, but once I've finished that I'll come back, study, and fix it in this PR.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 18:22):

:memo: alexcrichton submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 18:22):

:speech_balloon: alexcrichton created PR review comment:

That helps yeah, and perhaps this expression could be split up as:

// explanatory comment
let english_name = some && expression;
// .. repeated for "units" ..

// explanation
!english_name1 && english_name2

(or something like that)

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 19:33):

dicej updated PR #15336.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 19:38):

dicej commented on PR #15336:

@alexcrichton Thanks for the additional test; I've pushed an update that includes it and a fix for it. I woke up at about 2am last night thinking there was an issue with tasks getting stuck in the pending queue, but then talked myself out of it this morning; I guess I was right the first time.

I'll push another update to clarify the code in is_ready.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 19:44):

dicej updated PR #15336.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 19:44):

:memo: dicej submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 19:44):

:speech_balloon: dicej created PR review comment:

Done!

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 19:45):

:memo: dicej submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 19:45):

:speech_balloon: dicej created PR review comment:

Actually, I'll split it up a bit more.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 19:52):

dicej updated PR #15336.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 19:53):

:memo: dicej submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 19:53):

:speech_balloon: dicej created PR review comment:

OK, really done this time.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 19:56):

:thumbs_up: alexcrichton submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 19:56):

alexcrichton has enabled auto merge for PR #15336.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 20:03):

alexcrichton added PR #15336 fix CM async spec mismatch regarding backpressure-pending calls to the merge queue.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 20:26):

:check: alexcrichton merged PR #15336.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 18 2026 at 20:26):

alexcrichton removed PR #15336 fix CM async spec mismatch regarding backpressure-pending calls from the merge queue.


Last updated: Nov 19 2026 at 20:05 UTC