Repository navigation
fix(runtime): schedule threads during sync calls per the Canonical ABI - #2539
Merged
vados-cosmonic merged 15 commits intoNov 25, 2026
Merged
vados-cosmonic merged 15 commits into
vados-cosmonic merged 15 commits into
Conversation
This was referenced Nov 25, 2026
github-merge-queue
Bot
removed this pull request from the merge queue due to a conflict with the base branch
Nov 25, 2026
vados-cosmonic
enabled auto-merge
November 25, 2026 10:34
Only a task's implicit thread waits for its instance's exclusive lock. In the Canonical ABI the lock is taken when a task enters and checked when its callback is next delivered (`wait_from_callback`); threads made by `thread.new-indirect` never take or wait for it. The scheduler applied the lock check to every suspended thread, so an explicit thread whose readiness condition held stayed parked while another task held the lock.
`thread.suspend-then-promote` and `thread.yield-then-promote` switch to the target only if it is ready (Canonical ABI `Thread.ready`). For a thread parked waiting to deliver its task's next callback, readiness includes the instance's exclusive lock being free. Promotion checked only the thread's own condition, so it could switch into a callback thread while another task held the lock. It now uses the same readiness as the scheduler.
A built-in that can suspend was always invoked inside an async function, so a trap it raised before blocking became a rejected promise. The guest stack suspended on that promise instead of trapping, and the code that called into it carried on first: a caller whose callee traps in `thread.suspend-to` or `thread.yield-to` ran on to its own next instruction before the trap reached it. Invoke the built-in synchronously and rethrow a synchronous trap as one. A built-in that blocks still suspends the stack as before.
In the Canonical ABI, a call of a non-async-typed function runs until it returns, and while it is blocked only ready threads of the callee's instance may run (`canon_lift`, `not ft.async_`). If none is ready, the call traps with "cannot block a synchronous task before returning". The runtime decided ahead of time whether a sync-typed task could block and otherwise let every instance in the store keep running. Track calls of non-async-typed guest functions store-wide instead: - while one is in progress, the scheduler resumes only threads of its callee instance; other instances wait until it returns; - once nothing can make progress and such a call is still blocked, the idle check traps it with the spec's message rather than reporting a deadlock. Host operations still in flight defer the check, as for deadlocks.
The waitable intrinsics declared no global names, so an export named for one of them (`waitable-set-wait` becomes `waitableSetWait`) was bound to a local that shadowed the intrinsic. Trampolines calling the intrinsic then called the export instead, which recursed until the stack overflowed.
Bring the inline intrinsics in line with jco-cm-runtime: - only implicit threads wait for the exclusive lock; - `thread.*-then-promote` uses the scheduler's readiness, which includes the lock; - calls of non-async-typed guest functions are tracked store-wide, so that while one is in progress only its callee instance is scheduled, and one left blocked with nothing ready traps with "cannot block a synchronous task before returning"; - a suspending built-in that traps before blocking traps the calling stack at once. Also regenerate the embedded runtime.
Un-skip `during-sync-call-blocking-ops`, `during-sync-call-no-sibling-resume` and `switch-to-ready-callback`, which now pass. Two remain skipped for other issues, with corrected reasons: `during-sync-call-exclusive-resume` ends with a task whose last thread exits before it resolves (bytecodealliance#2534), and `trap-if-block-and-sync` expects "unsupported callback code" for a callback code above 2 (bytecodealliance#2537).
While a call of a non-async-typed function was in progress, the tick loop of every other instance stopped once it went idle, and ending the call restarted only loops with work that was ready at that moment. An instance whose threads became ready later, or never did, was left with no loop: readiness that nothing announced went unseen, and the store-wide idle check no longer ran, so a later deadlock hung instead of trapping. Held instances now keep polling. Their ticks still resume nothing during the call, and their idle check sees the store as the callee's does.
Mirror jco-cm-runtime in the inline intrinsics: the tick loop of an instance held back by a call of a non-async-typed function keeps polling instead of stopping, so that it sees readiness and runs the store-wide idle check both during the call and after it ends. Also regenerate the embedded runtime.
The backpressure intrinsics, `getOrCreateAsyncState`, `getErrorPayloadString`, `promiseWithResolvers` and the memory registry intrinsics declared no global names. An export named for one of them (`backpressure-inc` becomes `backpressureInc`) was bound to a local that shadowed the intrinsic, so a trampoline calling it called the export, which recursed until the stack overflowed.
A component exporting `backpressure-inc`, `backpressure-dec`, `get-or-create-async-state` and `promise-with-resolvers` calls the backpressure built-ins from each export; every call must reach the intrinsic rather than the export bound under the same name.
The new deadlock-check tests in component-scheduling slept 5ms and then expected the check to have run, which flaked in CI when the timer fired late. Wait for the trap where one is expected, and for the scheduled check to finish where none is.
…er of an exited callee With sync calls tracked store-wide, a non-async-typed caller that waits on a subtask whose threads all exited unresolved traps with "cannot block a synchronous task before returning" once nothing can run, as exit-without-resolving.wast expects, rather than reporting a deadlock.
…usive-resume Both were skipped because a synchronous call blocked with no ready thread reported a deadlock. It now traps with "cannot block a synchronous task before returning", and both fixtures pass.
vados-cosmonic
force-pushed
the
fix/sync-call-thread-scheduling
branch
from
November 25, 2026 11:14
7be780b to
83d4d88
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Resolves #2535
Brings thread scheduling during sync calls and under the exclusive lock in line with the Canonical ABI,
in both runtimes.
Blocking in a sync call is decided when it blocks (1).
A call of a non-async-typed function is now tracked store-wide from entry until its task resolves.
When such a call blocks,
its instance's ready threads run;
only once nothing can make progress does it trap with
"cannot block a synchronous task before returning" (
canon_lift,not ft.async_).That check lives in the idle/deadlock check,
so in-flight host operations defer it, as they defer deadlock detection.
mayBlock()and the transpile-time inference stay.They no longer decide whether a sync-typed call may block;
they decide whether its stack can suspend at all,
i.e. whether the export is JSPI-wrapped and Promise-returning.
A stack that cannot suspend still traps as soon as it would block.
Only the callee instance runs during a sync call (2).
While a sync call is in progress,
other instances' tick loops resume nothing;
ending the call nudges those with ready work.
They keep polling meanwhile,
so readiness nobody announces is still seen after the call,
and the store-wide idle check still runs.
thread.*-then-promoterespects the exclusive lock (3).isThreadReadyuses the scheduler's readiness, lock included (Thread.ready).The lock applies only to implicit threads (4).
Threads made by
thread.new-indirectnever wait for it.Two fixes the newly enabled WASTs needed:
It was called inside an async function,
so the trap became a rejected promise and the caller ran ahead first
(
switch-to-ready-callback).An export named
waitable-set-waitwas bound to a local that shadowed the intrinsic,so the trampoline called the export and recursed (
during-sync-call-blocking-ops).The same held for
backpressure-inc,backpressure-dec,get-or-create-async-state,promise-with-resolversand a few other intrinsic names, which are now reserved too.The embedded runtime is regenerated.
Tests.
during-sync-call-blocking-opsduring-sync-call-no-sibling-resumeswitch-to-ready-callbackduring-sync-call-exclusive-resume:its last case traps "task exited without resolution" (Task completion doesn't match component-model#767 #2534).
trap-if-block-and-sync:all of its sync-blocking cases now trap as expected;
one case still needs "unsupported callback code" (Async built-ins: missing traps, wrong trap kind, and check ordering #2537).
jco-cm-runtimefor each problem,including an instance held back by a sync call that must still poll after it,
and render assertions for the inline intrinsics.
p3/intrinsic-export-names: exports named after trampoline intrinsics.Known differences from the spec that remain.
one macrotask later,
and only once no host operation is in flight anywhere in the store.
An unrelated pending host call in another instance delays it until that call settles.
still resumes when that promise settles during a sync call;
only scheduler-driven resumption is held back.
Not addressed (could not be reproduced as separate, contained fixes):