Skip to content

fix(runtime): thread indexing, inline resumption and deadlock check fixes - #2543

Merged
vados-cosmonic merged 6 commits into
bytecodealliance:mainfrom
vados-cosmonic:fix/threads-audit-followups
Nov 25, 2026
Merged

vados-cosmonic merged 6 commits into
bytecodealliance:mainfrom
vados-cosmonic:fix/threads-audit-followups

Conversation

@vados-cosmonic

@vados-cosmonic vados-cosmonic commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Follow-ups to the scheduling work in #2535 and #2536.

Implicit thread index.
The Canonical ABI indexes a task's implicit thread in Task.enter_implicit_thread,
after any wait for backpressure or the exclusive lock.
Export calls indexed it at task creation,
so a call held back by backpressure took an index ahead of calls that entered before it.
Export tasks are now created with indexOnEntry and indexed as they enter;
a task that never enters releases nothing.
Guest-to-guest calls still index when the call is prepared:
their arguments are lowered (and the callee's realloc may run) before entry.

Inline JSPI resumption.
The inline import wrapper restored the task registers when the import's promise settled,
but the stack resumes in a later job.
Two stacks released in the same turn both restored their task before either ran,
so the first ran as the second's task.
Resumptions are now chained through the registers, as the external runtime already does.

Deadlock check and host-held channels.
A guest copy on a stream or future whose other end was lifted to the host
can only be completed by the host,
which may do so from untracked code (e.g. a timer).
The check trapped such a store as deadlocked.
A pending guest copy on a host-held channel now counts as a host operation until it finishes.
An end stays host-held while the host keeps it,
including when the host passes it back into a guest (jco pumps from it into a new end);
a write that nobody reads then waits rather than trapping.

Unhandled rejection after a deadlock.
A synchronous guest-to-guest call whose callee never entered
failed with the deadlock trap but left the callee's result promise unobserved,
which surfaced as an unhandled rejection.
That promise is now marked handled; the failure still reaches the caller through the call.

Tests:

  • test/p3/thread-indices.ts: a task held back by backpressure takes its index when it enters.
  • test/p3/deadlock-detection.ts: stream and future writes waiting on a host reader are not deadlocks;
    a sync call whose callee never enters fails without unhandled rejections.
  • intrinsics::tests string checks for the inline runtime.

The Canonical ABI gives a task's implicit thread its index in the
instance's thread table in `Task.enter_implicit_thread`, after any wait
for backpressure or the exclusive lock. The runtime indexed it when the
task was created, so a call held back by backpressure took an index
before calls that entered ahead of it, and `thread.index` disagreed with
the spec's numbering.

Export calls now create their task with `indexOnEntry`: the implicit
thread is indexed as the task enters, and a task that never enters
releases no index. Both the external and inline runtimes change, and
the export glue reads the thread ID after entry.
A stack suspended in a JSPI import resumes in a later job than the one
that settles the promise it waits on. The inline import wrapper restored
the task registers when that promise settled, so two stacks released in
the same turn both restored their task before either ran, and the first
then ran as the second's task.

Chain resumptions through the registers the way the external runtime
does: each stack restores its task only once every stack released before
it has run, and the next waits until this one has suspended again or
returned.
A guest copy on a stream or future whose other end was lifted to the
host can only be completed by the host, whenever it reads, writes or
drops that end. The host does that from its own code (e.g. a timer),
outside any tracked host operation, so the deadlock check saw only
suspended tasks and trapped a store that was waiting on the host.

Mark a stream or future as host-held when one of its ends is lifted to
the host, and count a pending guest copy on it as a host operation until
the copy finishes, in both the external and inline runtimes. The future
end class takes the host operation context as an optional second
argument for this.
A synchronous guest-to-guest call waits for its callee to enter before
it awaits the callee's result. When the store traps during that wait
(e.g. the deadlock check fires because the callee's instance is locked
for good), the callee's task is rejected, the call fails with the trap,
and the result promise, rejected too, was never observed: it surfaced
as an unhandled rejection after the trap had already been reported.

Mark the result promise as handled when it is created, in both the
external and inline runtimes; the failure still reaches the caller
through the call itself.
@vados-cosmonic
vados-cosmonic force-pushed the fix/threads-audit-followups branch from 481ba33 to 2283edb Compare November 25, 2026 13:31
Run rustfmt over the intrinsics test that checks a task releases the
thread index only when its implicit thread holds one.
@vados-cosmonic
vados-cosmonic added this pull request to the merge queue Nov 25, 2026
Merged via the queue into bytecodealliance:main with commit 5c8484c Nov 25, 2026
76 checks passed
@vados-cosmonic
vados-cosmonic deleted the fix/threads-audit-followups branch November 25, 2026 14:40
@jco-release-bot

jco-release-bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

🎉 The code in this PR was made available in the following release:
https://github.com/bytecodealliance/jco/releases/tag/jco-cm-runtime-v0.0.2

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.

2 participants