[WASM_WORKERS] Fix UB in pthread_mutex with debug+hybrid builds#26673
Merged
sbc100 merged 1 commit intoemscripten-core:mainfrom Apr 14, 2026
Merged
Conversation
Collaborator
Author
5e123c7 to
6434ea6
Compare
Collaborator
Author
|
@stephenduong1004 @walkingeyerobot I believe this is the fix we need to the issues we have been seeing internally with workers+pthreads hybrid builds. |
6434ea6 to
dfdda44
Compare
Collaborator
Author
|
I did end up finding a simpler/nicer repro test case for this. |
dfdda44 to
8ea380b
Compare
This fix only applies to the hybrid Wasm Workers + pthread build mode. We can't currently rely on calling `pthread_self()` from a Wasm Worker, even the hybrid mode. We are working on fixing that in emscripten-core#26631. However, we do have a CURRENT_THREAD_ID macro that works on both Wasm Workers and pthreads. As it happens, pthread locks under musl do not normally depend on `pthread_self` except for "recursive" which need to access the `pthread_self()->tid` field to track the current owner. This means that I broke the debug + hybrid mode locks in emscripten-core#24607 because I added the recursion check to all locks in debug builds. While we do have a repro case for this which current crashes its not directly testing the issue, it just heppens to crash due to the current undefined behavior when doing `pthread_self()->tid` from a Wasm Worker. Fixes: emscripten-core#26619
8ea380b to
35c6f61
Compare
tlively
approved these changes
Apr 14, 2026
Member
tlively
left a comment
There was a problem hiding this comment.
I don't suppose we could make __pthread_self()->tid work with Wasm Workers instead?
Collaborator
Author
We have a larger plan to try to make that work. See #26631. If we ever do make that work we can revert this partial solution. |
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.
This fix only applies to the hybrid Wasm Workers + pthread build mode.
We can't currently rely on calling
pthread_self()from a Wasm Worker, even the hybrid mode. We are working on fixing that in #26631. However, we do have aCURRENT_THREAD_IDmacro that works on both Wasm Workers and pthreads.As it happens, pthread locks under musl do not normally depend on
pthread_selfexcept for "recursive" which need to access thepthread_self()->tidfield to track the current owner. This means that I broke the debug + hybrid mode locks in #24607 because I added the recursion check to all locks in debug builds.While I have added a repro case for this its not directly testing the issue, it just heppens to crash due to the current undefined behavior when doing
pthread_self()->tidfrom a Wasm Worker.Fixes: #26619