MessageManager::Lock: Release the entry mutex whenever it was gained - #1752
Open
yumasansansan wants to merge 1 commit into
Open
yumasansansan wants to merge 1 commit into
yumasansansan wants to merge 1 commit into
Conversation
Lock gains its entry mutex in exclusiveTryAcquire(), and of that mutex the header says: "If multiple threads call enter() simultaneously, only one will succeed in gaining this mutex. The mutex is released again in exit()." It is not always released there. exit() releases it only when the lock was acquired, and there is one way to succeed without acquiring anything: tryAcquire() returns true straight away when the calling thread already has exclusive access to the MessageManager. Then acquired stays false, exit() returns before it makes the guard that releases the mutex, and the mutex is still held when the Lock is destroyed:
WARNING: ThreadSanitizer: destroy of a locked mutex
#0 pthread_mutex_destroy
juce-framework#1 juce::CriticalSection::~CriticalSection() juce_SharedCode_posix.h:50
juce-framework#2 juce::MessageManager::Lock::~Lock() juce_MessageManager.cpp:280
juce-framework#3 juce::MessageManagerLock::~MessageManagerLock() juce_MessageManager.cpp:449
juce-framework#4 juce::JuceVST3Component::LockedVSTComSmartPtr<juce::JuceAudioProcessor>::~LockedVSTComSmartPtr() juce_audio_plugin_client_VST3.cpp:3831
juce-framework#5 juce::JuceVST3Component::JuceVST3Component(...) juce_audio_plugin_client_VST3.cpp:2573
Destroying a locked mutex is undefined, and pthread_mutex_destroy() is allowed to answer EBUSY and leave the mutex as it was. The same invariant is broken the other way round as well: the blocking message can set acquired after tryAcquire() has given up waiting for it, and exit() would then release a mutex this object never gained.
What the entry mutex needs is its own answer to "was it gained", which is not the same question as "was the MessageManager locked". The Lock remembers that it gained the mutex and releases it on that; acquired keeps its meaning, and the cleanup that belongs to it -- the blocking message, the thread that holds the MessageManager -- is unchanged. Only the thread holding the entry mutex touches the new member, so it needs no mutex of its own.
Every VST3 plug-in built with JUCE goes through it: JuceVST3Component's constructor makes a LockedVSTComSmartPtr, and that smart pointer's destructor takes a MessageManagerLock on a thread which, in a host, already holds the message manager lock. ADLplug-Next found it with the thread sanitizer, in its own renderer, which loads the VST3 the build has just made; the report is the first thing that happens after the plug-in is created.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
MessageManager::Lockgains its entry mutex inexclusiveTryAcquire(), and of that mutex the header says: "If multiple threads call enter() simultaneously, only one will succeed in gaining this mutex. The mutex is released again in exit()." It is not always released there.exit()releases it only when the lock was acquired, and there is one way to succeed without acquiring anything:tryAcquire()returnstruestraight away when the calling thread already has exclusive access to theMessageManager. Thenacquiredstays false,exit()returns before it makes the guard that releases the mutex, and the mutex is still held when theLockis destroyed:Destroying a locked mutex is undefined, and
pthread_mutex_destroy()is allowed to answerEBUSYand leave the mutex as it was. The same invariant is broken the other way round as well: the blocking message can setacquiredaftertryAcquire()has given up waiting for it, andexit()would then release a mutex the object never gained.What the entry mutex needs is its own answer to "was it gained", which is not the same question as "was the
MessageManagerlocked". TheLockremembers that it gained the mutex and releases it on that;acquiredkeeps its meaning, and the cleanup that belongs to it is unchanged. Only the thread holding the entry mutex touches the new member, so it needs no mutex of its own.Every VST3 plug-in built with JUCE goes through it:
JuceVST3Component's constructor makes aLockedVSTComSmartPtr, and that smart pointer's destructor takes aMessageManagerLockon a thread which, in a host, already holds the message manager lock. ADLplug-Next found it with the thread sanitizer, in its own renderer, which loads the VST3 the build has just made; the report is the first thing that happens after the plug-in is created.It is in 9.0.2 and in
developas it stands, and it is independent of the commits of ours that are already open (#1749, #1750).