Deadlock in Typeface::findTypeFaceFor (minimal repro)

The following console program deadlocks for me on macOS 10.14 almost every time:

#include <JuceHeader.h>

static void makeImages()
{
    for (int i = 0; i < 10000; ++i)
    {
        juce::Image img (juce::Image::PixelFormat::RGB, 300, 300, false);
        juce::Graphics g (img);
        g.setColour (juce::Colours::black);
        g.fillAll();
    }
}

int main (int argc, char* argv[])
{
    juce::WaitableEvent event;
    juce::Thread::launch ([&event] {
        makeImages();
        event.signal();
    });

    makeImages();
    event.wait();

    return 0;
}

It seems the culprit is Typeface::findTypefaceFor. If I wrap the top part of the function (the part protected by a ScopedReadLock) in a {} scope block, the deadlock goes away, but I don’t understand why. Is this some kind of bug in the interaction between a ScopedReadLock and a ScopedWriteLock?

  • If a thread already has a read lock and tries to obtain a write lock, it will succeed if there are no other readers

I think both threads acquire a read lock successfully, but then are unable to acquire the write lock later on in the function because multiple threads own read locks. By giving up the read lock before trying to take the write lock, one of the threads should be able to acquire the write lock without problems.

Thanks for reporting!

Ah, that makes a lot of sense yes. Do please post here when the fix makes it to develop!

The fix is now on develop and juce6:

Thank you so much!