emscripten-core / emscripten-core/emscripten

ASSERTIONS mode should tell the user when trying to use pthreads w/o -pthread

Open
#18,240 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

I just tried building [simple C++ example](https://web.dev/webassembly-threads/#c-2) that uses threads and trying to run, getting an anonymous abort somewhere in Emscripten internals:

```js
> node temp
Before the thread
Aborted(native code called abort())
/home/rreverser/temp.js:147
throw ex;
^

RuntimeError: Aborted(native code called abort())
at abort (/home/rreverser/temp.js:990:11)
at _abort (/home/rreverser/temp.js:1304:7)
at std::__2::__throw_system_error(int, char const*) (:wasm-function[1689]:0x24cef)
at std::__2::thread::thread(main::$_0&&) (:wasm-function[15]:0x1167)
at __original_main (:wasm-function[14]:0xfff)
at main (:wasm-function[135]:0x3e75)
at /home/rreverser/temp.js:1034:22
at callMain (/home/rreverser/temp.js:4806:15)
at doRun (/home/rreverser/temp.js:4860:23)
at run (/home/rreverser/temp.js:4875:5)
```

After a few minutes of cleaning up `emcc` cache & rebuilding (in case it was my local pthread-related experiments that broke something), I finally realised that I just forgot to add `-pthread` flag when building 🤦‍♂️

So Emscripten linked it with the `library_pthread_stub.js` instead, which has most operations stubbed out.

I think in the default `ASSERTIONS` mode it would be worth to either print a warning or even throw an early error when `pthread_create` or other unsupported operations are invoked.

Also, generally, `__throw_system_error` probably shouldn't ignore the message parameter in non-exceptions mode and instead pass it to be printed by JS here: https://github.com/emscripten-core/emscripten/blob/2e02d0eba92c9c7489bed38eff9bd1ee61e93229/system/lib/libcxx/src/system_error.cpp#L294. The cost of passing it would be negligible, but error message at least wouldn't be lost.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.