Skip to content

[WASM_WORKERS] Fix UB in pthread_mutex with debug+hybrid builds#26673

Merged
sbc100 merged 1 commit intoemscripten-core:mainfrom
sbc100:fix_recusrive_mutex_worker_hybrid_debug
Apr 14, 2026
Merged

[WASM_WORKERS] Fix UB in pthread_mutex with debug+hybrid builds#26673
sbc100 merged 1 commit intoemscripten-core:mainfrom
sbc100:fix_recusrive_mutex_worker_hybrid_debug

Conversation

@sbc100
Copy link
Copy Markdown
Collaborator

@sbc100 sbc100 commented Apr 13, 2026

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 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 #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()->tid from a Wasm Worker.

Fixes: #26619

@sbc100
Copy link
Copy Markdown
Collaborator Author

sbc100 commented Apr 13, 2026

@slowriot

@sbc100 sbc100 requested review from dschuff and kripken April 13, 2026 22:47
@sbc100 sbc100 force-pushed the fix_recusrive_mutex_worker_hybrid_debug branch from 5e123c7 to 6434ea6 Compare April 13, 2026 22:50
@sbc100
Copy link
Copy Markdown
Collaborator Author

sbc100 commented Apr 13, 2026

@stephenduong1004 @walkingeyerobot I believe this is the fix we need to the issues we have been seeing internally with workers+pthreads hybrid builds.

@sbc100 sbc100 changed the title [WASM_WORKERS] Fix pthread_mutex API in debug hybrid builds [WASM_WORKERS] Fix UB in pthread_mutex with debug+hybrid builds Apr 13, 2026
@sbc100 sbc100 force-pushed the fix_recusrive_mutex_worker_hybrid_debug branch from 6434ea6 to dfdda44 Compare April 13, 2026 23:18
@sbc100
Copy link
Copy Markdown
Collaborator Author

sbc100 commented Apr 13, 2026

I did end up finding a simpler/nicer repro test case for this.

@sbc100 sbc100 force-pushed the fix_recusrive_mutex_worker_hybrid_debug branch from dfdda44 to 8ea380b Compare April 13, 2026 23:23
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
@sbc100 sbc100 force-pushed the fix_recusrive_mutex_worker_hybrid_debug branch from 8ea380b to 35c6f61 Compare April 13, 2026 23:24
Copy link
Copy Markdown
Member

@tlively tlively left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't suppose we could make __pthread_self()->tid work with Wasm Workers instead?

@sbc100
Copy link
Copy Markdown
Collaborator Author

sbc100 commented Apr 14, 2026

I don't suppose we could make __pthread_self()->tid work with Wasm Workers instead?

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.

@sbc100 sbc100 merged commit ef94d08 into emscripten-core:main Apr 14, 2026
27 checks passed
@sbc100 sbc100 deleted the fix_recusrive_mutex_worker_hybrid_debug branch April 14, 2026 05:36
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.

Regression 4.0.10 to 4.0.11: -pthread + -sWASM_WORKERS aborts on trivial allocation/deallocation workload

2 participants