Skip to content

[WASM_WORKERS] Don't define pthread function in WASM_WORKERS builds#26336

Merged
sbc100 merged 1 commit intoemscripten-core:mainfrom
sbc100:wasm_workers_pthread_api
Feb 26, 2026
Merged

[WASM_WORKERS] Don't define pthread function in WASM_WORKERS builds#26336
sbc100 merged 1 commit intoemscripten-core:mainfrom
sbc100:wasm_workers_pthread_api

Conversation

@sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Feb 25, 2026

Using the pthread API with WASM_WORKERS simply will not work.

This also means function-local-static C++ initializes will now fail to link with WASM_WORKERS, at least until we fix #26277. Note that prior to this change this was already broken but would fail at runtime.

Note that its still possible to build with WASM_WORKERS + pthreads, in which case all the real pthread functions are available, but calling them from a Wasm Worker would be undefined behaviour (with or without this PR).

See #26314

@sbc100 sbc100 force-pushed the wasm_workers_pthread_api branch from 1423e8c to c899a02 Compare February 25, 2026 21:24
Using the pthread API with WASM_WORKERS simply will not work.
@sbc100 sbc100 force-pushed the wasm_workers_pthread_api branch from c899a02 to 986ba39 Compare February 25, 2026 21:38
@sbc100 sbc100 requested review from dschuff and kripken February 25, 2026 22:19
@sbc100 sbc100 enabled auto-merge (squash) February 25, 2026 22:41
@dschuff
Copy link
Member

dschuff commented Feb 25, 2026

Is it possible to create a wasm worker in a build that uses pthreads (was that ever possible)?

@sbc100
Copy link
Collaborator Author

sbc100 commented Feb 25, 2026

Is it possible to create a wasm worker in a build that uses pthreads (was that ever possible)?

Yes, in that case you get all the pthread functions linked in.

They exist, but bad things would happen if you called them from wasm worker.

We don't have any protection against that though, either before of after this PR.

I will update the PR description.

5.0.3 (in development)
----------------------
- When building with `-sWASM_WORKERS` emscripten will no longer include pthread
API stub functions. These stub functions where never designed to work under
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
API stub functions. These stub functions where never designed to work under
API stub functions. These stub functions were never designed to work under

@sbc100 sbc100 merged commit c01004b into emscripten-core:main Feb 26, 2026
36 checks passed
@sbc100 sbc100 deleted the wasm_workers_pthread_api branch February 26, 2026 00:09
@juj
Copy link
Collaborator

juj commented Feb 26, 2026

Hmm, it looks like this isn't quite as simple. After this PR, this code fails:

a.cpp

#include <emscripten/html5.h>

int foo(int x)
{
  static int z = emscripten_performance_now();
  ++z;
  if (x <= 0) return 0;
  return foo(x-1) + foo(x-2) + z;
}

int main()
{
  return foo(42);
}

em++ a.cpp -o a.html -sWASM_WORKERS

on

wasm-ld: error: C:\emsdk\emscripten\main\cache\sysroot\lib\wasm32-emscripten\libc++abi-debug-ww-noexcept.a(cxa_guard.o): undefined symbol: pthread_cond_wait
wasm-ld: error: C:\emsdk\emscripten\main\cache\sysroot\lib\wasm32-emscripten\libc++abi-debug-ww-noexcept.a(cxa_guard.o): undefined symbol: pthread_mutex_lock
wasm-ld: error: C:\emsdk\emscripten\main\cache\sysroot\lib\wasm32-emscripten\libc++abi-debug-ww-noexcept.a(cxa_guard.o): undefined symbol: pthread_mutex_unlock
wasm-ld: error: C:\emsdk\emscripten\main\cache\sysroot\lib\wasm32-emscripten\libc++abi-debug-ww-noexcept.a(cxa_guard.o): undefined symbol: pthread_cond_broadcast

@juj
Copy link
Collaborator

juj commented Feb 26, 2026

Maybe we should then contend to routing these functions to a partial stub implementation in Wasm Workers mode, so that the compiler emitted constructs will be there.

@sbc100
Copy link
Collaborator Author

sbc100 commented Feb 26, 2026

Yes, I believe that is the exact issue described in #26277.

That code has always been broken under Wasm Workers, but would fail in subtle ways at runtime before (because the locked around the initializer was not safe). Now we get a nice build time failure, which I think its better than a broken program.

I believe fixing #26277 should not be too hard now that we have the futex API available.

@AndreyRepko would you have time to update your PR (#26283) to use the futex API?

@juj
Copy link
Collaborator

juj commented Feb 26, 2026

That code has always been broken under Wasm Workers

Oh right, yeah indeed, before that the pthread lock functions would come out as no-ops.

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.

__cxa_guard_acquire does not work WASM Workers (depends on pthread functions)

4 participants