You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SDL_CondWait* results in deadlocks when compiled with emscripten #5428
I have a repro here: https://gist.github.com/connorjclark/75c41ea6f3095b73dfb1815834e8761e – it is a test program that is composed of two worker threads, one produces jobs, the other consumes jobs. It uses a condition variable to orchestrate. The first file is implemented with pthread directly and works as expected on my mac and via emscripten. The second file uses equivalent SDL functions, and that works on my mac, but on the web via emscripten a deadlock occurs.
You'll notice some commented out lines of code–those are just slightly different ways to construct the locking, and they result in a deadlock too (but not on my mac).
Note: surge.sh does not set the necessary headers for SAB, but you can use an extension like this to set Cross-Origin-Embedder-Policy=require-corp and Cross-Origin-Opener-Policy=same-origin
(FWIW: refresh enough and you'll see 2 jobs processed before the deadlock)
The deadlock does not occur if SDL_CondWaitTimeout (and its associated while loop) is not present (so the basic mutex lock/unlock work fine). Of course, the shared state is so trivial that concurrent reads/writes aren't noticeable–but this is because the example is purposefully simple.
Given the SDL_syscond.c is so simple (just a wrapper around pthread), I believe the issue must be in SDL_mutex.c. My first impression is that the "fake recursive" lock functionality does not work with condition variables:
I compiled SDL locally without the "fake recursive" feature and the deadlock went away! So this is happening:
thread A gets the lock, sets the mutex owner to itself
thread B requests lock, is blocked and waits
thread A calls pthread_cond_timedwait which releases the lock... but the "owner" field in SDL struct is still set to thread A
thread B gets the lock, sets the mutex owner to itself
thread B signals on the condition variable, then releases the lock, setting the mutex own to nil
thread A is woken up via the condition variable, reaquires the lock (who has no owner in the SDL struct)
thread A attempts to release the lock, but according to the SDL struct it doesn't "own" it so an error is returned. so now the lock is retained accidentally
deadlock
I modified my example program to print the error code after each unlock, and saw "mutex not owned by this thread".
Note: anyone wanting to workaround this for emcc, I've provided what I'm doing in my build script for now:
# temporary workaround until fixed upstream# emcc's cache will require you to manually do this (one time) for this to be picked up:# embuilder build sdl2# <run that sed command below># rm -rf "$(dirname $(which emcc))/cache/sysroot/lib/wasm32-emscripten"# Now you can run your build commands as normal.# see https://github.com/libsdl-org/SDL/issues/5428
sed -i -e 's/#define FAKE_RECURSIVE_MUTEX 1//'$(dirname $(which emcc))/cache/ports/sdl2/SDL-release-2.0.20/src/thread/pthread/SDL_sysmutex.c
It's nearly what SDL is doing already, except this mentions using a condition variable to negotiate the entire process, and SDL only uses one for part of it. I'll try to prepare a PR that implements the fake recursive lock as described by the link.
This can be closed now, just want to call out this potential issue that might need an issue filed first:
Unfortunately, SDL has already painted itself into a corner by defining SDL_mutex to be a recursive (or reentrant) lock. For SDL 3, it might be a good idea to do what dbus did, and distinguish between "mutex that is compatible with condition variables but not necessarily recursive" and "mutex that is guaranteed to be recursive"?
Activity
icculus commentedon Mar 21, 2022
Tossing this into 2.0.24 unless @Daft-Freak feels like looking into it sooner.
connorjclark commentedon Mar 21, 2022
Given the SDL_syscond.c is so simple (just a wrapper around pthread), I believe the issue must be in SDL_mutex.c. My first impression is that the "fake recursive" lock functionality does not work with condition variables:
SDL/src/thread/pthread/SDL_sysmutex.c
Lines 92 to 106 in a5c610b
I compiled SDL locally without the "fake recursive" feature and the deadlock went away! So this is happening:
I modified my example program to print the error code after each unlock, and saw "mutex not owned by this thread".
Note: anyone wanting to workaround this for emcc, I've provided what I'm doing in my build script for now:
connorjclark commentedon Apr 1, 2022
There is a useful summary of how to emulate recursive mutexes on wikipedia: https://en.wikipedia.org/wiki/Reentrant_mutex#:~:text=Software%20emulation%5Bedit,structure%3A%5Bcitation%20needed%5D
It's nearly what SDL is doing already, except this mentions using a condition variable to negotiate the entire process, and SDL only uses one for part of it. I'll try to prepare a PR that implements the fake recursive lock as described by the link.
Compile with recursive mutexes for emscripten
Compile with recursive mutexes for emscripten
Compile with recursive mutexes for emscripten
Compile with recursive mutexes for emscripten
connorjclark commentedon May 24, 2022
This can be closed now, just want to call out this potential issue that might need an issue filed first:
#5479 (comment)
icculus commentedon May 24, 2022
I dropped a link into the SDL3 infodump issue. Closing this one!
SDL 2.0.22 (libsdl-org#46)