Skip to content

SDL_CondWait* results in deadlocks when compiled with emscripten #5428

Closed

Description

@connorjclark

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).

I've upload the deadlocking example here: https://fallacious-bean.surge.sh/pthread.html

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

image

(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.

Activity

  1. added this to the 2.0.24 milestone on Mar 21, 2022
  2. icculus commented on Mar 21, 2022

    @icculus
    Collaborator

    Tossing this into 2.0.24 unless @Daft-Freak feels like looking into it sooner.

  3. connorjclark commented on Mar 21, 2022

    @connorjclark
    ContributorAuthor

    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:

    this_thread = pthread_self();
    if (mutex->owner == this_thread) {
    ++mutex->recursive;
    } else {
    /* The order of operations is important.
    We set the locking thread id after we obtain the lock
    so unlocks from other threads will fail.
    */
    if (pthread_mutex_lock(&mutex->id) == 0) {
    mutex->owner = this_thread;
    mutex->recursive = 0;
    } else {
    return SDL_SetError("pthread_mutex_lock() failed");
    }
    }

    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
  4. connorjclark commented on Apr 1, 2022

    @connorjclark
    ContributorAuthor

    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.

  5. connorjclark commented on May 24, 2022

    @connorjclark
    ContributorAuthor

    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"?

    #5479 (comment)

  6. icculus commented on May 24, 2022

    @icculus
    Collaborator

    I dropped a link into the SDL3 infodump issue. Closing this one!

  7. added a commit that references this issue on Mar 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions