Skip to content

[Pthread] : deadlock after pthread_create()

From emscripten documentation:

When pthread_create() is called, if we need to create a new Web Worker, then that requires returning the main event loop. That is, you cannot call pthread_create and then keep running code synchronously that expects the worker to start running - it will only run after you return to the event loop. This is a violation of POSIX behavior and will break common code which creates a thread and immediately joins it or otherwise synchronously waits to observe an effect such as a memory write. [...]

PTHREAD_POOL_SIZE is normally optional, but in our case it seems it prevents this behavior :

After vlc_clone we do not return to the main loop fast enough to allow vlc_join to actually join a pthread (chances are it won't be created).

on the newest emsdk (2.0.17), we have this warning if pthread_create is called and there are no more workers in the pool. We need to know the maximum number of threads we will spawn in advance (which is not good)

This is just a warning, the default behavior of PTHREAD_POOL_SIZE_STRICT is to warn and keep on doing whatever the code is supposed to be doing.

Tried to spawn a new thread, but the thread pool is exhausted.
This might result in a deadlock unless some threads eventually exit or the code explicitly breaks out to the event loop.
If you want to increase the pool size, use setting `-s PTHREAD_POOL_SIZE=...`.
If you want to throw an explicit error instead of the risk of deadlocking in those cases, use setting `-s PTHREAD_POOL_SIZE_STRICT=2`.
The default behavior is -s  PTHREAD_POOL_SIZE_STRICT=1, which will warn and create the worker anyway

TLDR; Not relying on the pthread implementation to create a thread and preallocating workers hides (one or more deadlocks)

Possibles solutions:

  • move the main() into a pthread (PROXY_TO_PTHREAD) -> it should keep operations (log, worker init) out of the UI thread

I tested that and the deadlock is still there. (at best, the demux will start but not the decoder threads).

  • synchronize vlc's pthread code with the js main_loop -> do not vlc_join() unless the spawned worker after vlc_clone() requested it is ready
Edited by Mehdi Sabwat