emscripten-core / emscripten-core/emscripten

Worker threads not being not recreated.

Open
#17,363 1 comment 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

Hello!

I've been struggling with this for several days now, please bare with me as I'm not a c developer.

I'm trying to fix an issue w/ https://github.com/ffmpegwasm/ffmpeg.wasm-core

After running ffmpeg.run ~100 times my browser tab crashes with a SIGILL message.

Workers just keep growing until tab crashes.
![image](https://user-images.githubusercontent.com/4172067/177017922-f6d961fb-d74f-43fa-bd82-81de486f3053.png)

To try to fix the issue, I've done the following:

I've cloned and rolled back to the released version on npm w/ a working build (b59fb55619d22681d0e4da83023e61b663f3645a)

This compiles ffmpeg with the following options ([link to the build script](https://github.com/ffmpegwasm/ffmpeg.wasm-core/blob/b59fb55619d22681d0e4da83023e61b663f3645a/wasm/build-scripts/build-ffmpeg.sh))
```
-s USE_SDL=2 # use SDL2
-s USE_PTHREADS=1 # enable pthreads support
-s PROXY_TO_PTHREAD=1 # detach main() from browser/UI main thread
-s INVOKE_RUN=0 # not to run the main() in the beginning
-s EXIT_RUNTIME=1 # exit runtime after execution
-s MODULARIZE=1 # use modularized version to be more flexible
-s EXPORT_NAME="createFFmpegCore" # assign export name for browser
-s EXPORTED_FUNCTIONS="[_main, _proxy_main]" # export main and proxy_main funcs
-s EXTRA_EXPORTED_RUNTIME_METHODS="[FS, cwrap, ccall, setValue, writeAsciiToMemory]" # export preamble funcs
-s INITIAL_MEMORY=2146435072 # 64 KB * 1024 * 16 * 2047 = 2146435072 bytes ~= 2 GB
```

I noticed that it's on an old version of emscripten (2.0.8)
- I've tried updating to latest but the build fails in a way that I wouldn't be able to fix in a reasonable amount of time.
- I've instead updated to 2.0.15, which from the release log of emscripten contains a couple of fixes for pthread exits.

Updating to 2.0.15 doesn't fix the issue itself, but actually breaks the implementation because for some reason, [they're calling `proxy_main` manually](https://github.com/ffmpegwasm/ffmpeg.wasm/blob/master/src/createFFmpeg.js#L91) instead of `callMain`. I think this is actually the cause of the original issue, I'm guessing `callMain` does some thread maintenance under the hood.

So I've updated the build from ` -s EXPORTED_FUNCTIONS="[_main, _proxy_main]"` to ` -s EXPORTED_FUNCTIONS="[_main]"`, then removed [this line](https://github.com/ffmpegwasm/ffmpeg.wasm/blob/master/src/createFFmpeg.js#L91), added `callMain` to the list of `EXTRA_EXPORTED_RUNTIME_METHODS`, and am instead calling `Core.callMain(args);` [here](https://github.com/ffmpegwasm/ffmpeg.wasm/blob/master/src/createFFmpeg.js#L132)

I've also removed the preceding `'./ffmpeg'` argument [here](https://github.com/ffmpegwasm/ffmpeg.wasm/blob/master/src/config.js#L4), as it was being passed to ffmpeg.

**So after making all of these changes**

I am able to successfully call ffmpeg, but only once. The worker threads are created, then exit (pretty sure this is what I want), and when calling `ffmpeg.run` again (which calls `Core.callMain(args)`), no new workers are created and it just hangs.

In addition to the above, I have also tried setting `EXIT_RUNTIME=0`, but w/ this setting the original issue happens again. Lots of workers created that never go away.

**What's the correct way of being able to run `main` more than once?**

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.