Skip to content

[pthreads] Fix relaying of mailbox notifications to main thread#27336

Merged
sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:fix-27037
Jul 14, 2026
Merged

[pthreads] Fix relaying of mailbox notifications to main thread#27336
sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:fix-27037

Conversation

@sbc100

@sbc100 sbc100 commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

In #27018, d.targetThread && d.targetThread != _pthread_self() in worker.onmessage on the main thread was changed to only check d.targetThread before relaying or asserting.

However, when waitAsyncPolyfilled is active or Atomics.waitAsync is unsupported (e.g., Firefox before 145), thread->waiting_async is not set when _emscripten_thread_mailbox_await runs. In this fallback mode, emscripten_thread_mailbox_send calls
_emscripten_notify_mailbox_postmessage, which sends a postMessage from the worker with targetThread set to the main thread.

When worker.onmessage on the main thread receives this message, d.targetThread == _pthread_self(). Without checking d.targetThread != _pthread_self(), the main thread either triggers an assertion failure (assert(d.targetThread != _pthread_self())) or drops the message when looking up PThread.pthreads[targetThread].

Restoring d.targetThread != _pthread_self() ensures that mailbox notifications addressed to the main thread fall through and are processed directly (checkMailbox()) instead of being forwarded.

Also add a browser test (browser.test_pthread_no_waitasync) that disables Atomics.waitAsync in a --pre-js script to verify this fallback behavior.

Fixes: #27037

@sbc100 sbc100 requested review from juj and kripken July 13, 2026 22:07
In emscripten-core#27018, `d.targetThread && d.targetThread != _pthread_self()` in
`worker.onmessage` on the main thread was changed to only check
`d.targetThread` before relaying or asserting.

However, when `waitAsyncPolyfilled` is active or `Atomics.waitAsync` is
unsupported (e.g., Firefox before 145), `thread->waiting_async` is not
set when `_emscripten_thread_mailbox_await` runs. In this fallback
mode, `emscripten_thread_mailbox_send` calls
`_emscripten_notify_mailbox_postmessage`, which sends a `postMessage`
from the worker with `targetThread` set to the main thread.

When `worker.onmessage` on the main thread receives this message,
`d.targetThread == _pthread_self()`. Without checking
`d.targetThread != _pthread_self()`, the main thread either triggers
an assertion failure (`assert(d.targetThread != _pthread_self())`)
or drops the message when looking up `PThread.pthreads[targetThread]`.

Restoring `d.targetThread != _pthread_self()` ensures that mailbox
notifications addressed to the main thread fall through and are
processed directly (`checkMailbox()`) instead of being forwarded.

Also add a browser test (`browser.test_pthread_no_waitasync`) that
disables `Atomics.waitAsync` in a `--pre-js` script to verify this
fallback behavior.

Fixes: emscripten-core#27037
@sbc100 sbc100 merged commit 8d056a6 into emscripten-core:main Jul 14, 2026
39 checks passed
@sbc100 sbc100 deleted the fix-27037 branch July 14, 2026 16:23
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.

browser.test_cross_origin regression: "Expected to find '/report_result?exit:0 ' in '/report_result?abort:Assertion failed"

2 participants