Repository navigation
Threading model pains #394
Replies: 6 comments
|
Just a quick edit for anyone who happens to read this. Fixing this solved lots of issues: #395. The get_value call is frequent and therefore we would get contention-failures in lots of places because the logging system I use only captures whatever the 2nd call is that would contend with the main thread. Another fix solved the issues of AU destroy mentioned in the original post: #400 However, I still get concurrent host calls in the in these hosts: pluginval and "My VST3 HostApplication". "My VST3 HostApplication" is the name that a JUCE-based host uses - see JUCE source code. As it's a non-specific name, it's unclear whether it's host used by real producers and composers or if it's a hobby-scale host (which we don't care so much about supporting). For this reason. I wrap all my main-thread calls in the mutex. It's relatively inexpensive and then I just don't need to worry about this problem any more. |
|
Thanks for writing this up. We're a small audio software company and big fans of the CLAP ecosystem. We're building a CLAP-first plugin in Rust using clack, with an architecture inspired by rust-clap-first-example. Bindings like clack tie the Beyond the current state, I’d like to understand the long-term design philosophy: where should the boundary lie between the wrapper’s responsibility and the plugin’s? Is @SamWindell's defensive mutex approach a temporary workaround, or is it fundamentally infeasible for the wrapper to solve, meaning plugins should permanently guard themselves? I'd like to use your answer to guide our project's architecture decisions. |
|
I'll just update with what I currently use with regard to this main-thread mess: https://github.com/floe-audio/Floe/blob/562c19616e30b45a54db07bf179d8e190595b487/src/os/threading.cpp#L12-L63 My plugin runs successfully in all the major hosts I have tested with this code in place. I have just accepted that hosts do not always follow the rules. In every aspect of my plugin's API boundary I am checking everything - pointers, state, logical threads, out of bounds, etc. I have automatic error reporting in my plugin and there's a large amount of (normally minor) host misbehaviour. You need to be very defensive and robust. Sadly I think the key thing to accept in order to get successful widespread compatibility is that some hosts may have multiple 'main threads'. It's nearly always true (but I still don't trust it) that all API's in the CLAP spec marked as As far as I remember, the CLAP spec says the main thread should actually be the main thread, but I think it's not often the case in reality - CLAP native or CLAP-wrapped. That's my two cents! |
|
The purpose of the clap-wrapper is to make CLAP plugins available in other plugin formats. These formats differ in semantics, calling sequences, and often carry legacy behavior, especially VST3, where host implementations vary widely. I don’t know the details of clack, but note that in CLAP the thread names are semantical, not necessarily tied to physical threads. Because of that, I’m not sure your approach will work reliably. Instantiation and calling sequences also differ between formats, so there is always some degree of impedance mismatch. The wrapper tries to bridge these differences and indicates the threading through the thread extension. A host is typically responsible for ensuring that UI-thread calls are not executed concurrently, and the same applies to audio-thread operations. As long as the host behaves correctly, the wrapper will behave correctly as well. The reality of host behavior across all plugin formats is difficult, something you encounter as soon as you write your own wrappers or core formats. With clap-wrapper, I am trying to solve as much of this as possible so that a CLAP plugin developer doesn’t need to worry about the other formats. However, when using clap-wrapper, you still need to pay attention to certain details and avoid being overly creative just because the CLAP spec appears to allow it. If you find an reproducable issue with clap-wrapper, feel free to open an issue with specifics. And regarding the mentioned “My VST3 HostApplication”: if it does things that violate the VST3 contract, I would consider it broken. |
|
I agree - threads must be treated as semantical if you want to have the best successful compatibility with both CLAP hosts and VST3/AU hosts. It's not normally hard to achieve to make this concession. To be clear though, the confusion is real with regards to the main-thread. The CLAP spec says this: As an aside, I never had a good time trusting the result of CLAP_EXT_THREAD_CHECK is_main_thread in clap-wrapper. I had to skip this check in my code. But hard to say if that's broken hosts, VST3/AU threading models or something else. |
|
First of all, I apologize for the delayed response. We’ve been swamped with an upcoming release. Thank you both for your detailed replies! As a result of our recent work, our plugin is now running stably across quite a few hosts. However, we are still struggling with how to handle the main thread correctly and have had to apply some temporary patches. For example, on macOS, we are synchronously marshaling method calls from certain hosts to the main thread using GCD ( It seems the overall issue can be broken down into several distinct problems. Moving forward, we want to take some time to organize them and consider, for each one, whether we should solve it by reviewing our own design and implementation, or if it warrants opening an issue on the clap-wrapper repository.
I completely agree with you, @SamWindell. While I fully understand the reality that there is often a gap between ideal host implementations and what happens in practice, the current wording in the spec does invite confusion. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I wanted to start an additional discussion regarding thread-check in the CLAP wrapper.
Oh boy, it's painful trying to make this all work robustly with CLAP, VST3, AU and misbehaving hosts.
CLAP versus the other formats seems to have slightly different threading models.
I'll just focus on the main-thread (AKA UI thread) for now.
CLAP model
CLAP spec says this about the main-thread: "This is the thread in which most of the interaction between the plugin and host happens. This will be the same OS thread throughout the lifetime of the plug-in."
That means its the same ID, right?
Link
VST3 model
VST3 just says this:
Vst:: IAudioProcessor::process: which could be called in an Audio Thread (sometime refered as realtime/processing thread), avoid any memory allocation!
Vst:: IAudioProcessor::setProcessing: which could be called in an Audio Thread, avoid any memory allocation!
Link
AU model
Not sure - documentation seems sparse. It seems they have the optional ability to mutex-protect some functions? Do we need this? SetMutex used in a guard before lots of callbacks such as here. Does that suggest the threading model is the wild west and mutex protecting everything is needed?
The reality
Using CLAP-wrapper's thread-check extension, I got these results in certain situations:
There are likely more cases than this.
The CLAP-wrapper thread-check extension uses 2 methods to try and give you the right result if you ask it is_main_thread: check against a thread ID, and check against thread_local 'overrides'.
For me, this is proof enough that VST3/AU hosts do not follow CLAP's model: that it's a single consistent thread.
Are hosts instead following a different model? That the main-thread can change IDs so long as there is only one main-thread at at time. A kind of 'logical' main thread. It's not clear. Maybe they are just calling the functions from different threads with no regard for if it's main-thread or not (hopefully not).
For now, I cannot trust the CLAP-wrapper thread-check extension and so I do not use it when the code detects it's in a VST3/AU wrapper.
Tests using thread-safety detection
I have been experimenting with a thread-safety detection system. Every CLAP main-thread callback enters and leaves a 'detection zone'. https://github.com/floe-audio/Floe/blob/1f7f4c7a7bacf90b4d41b6db5815765f6c02f65b/src/os/threading.cpp#L11-L32
Initial results:
Note these errors simply represent that at the point of trying to enter the 'logical main thread', another thread already got there. So either that callback or this callback is incorrect.
Unless I've got something wrong, these confirm the presence of race-conditions.
gui.destroy #0: multiple main threads
CLAP-as-AUv2 in Pluginval
params.get_value #0: multiple main threads
CLAP-as-AUv2 in Pluginval
params.get_value #0: multiple main threads
CLAP-as-AUv2 in Logic Pro 10.7.2
Possibly due to either get_value or state.load called from parallel 'main threads'
EDIT: Logic Pro is calling GetParameterInfo from the audio thread. get_value is called from a thread called
com.apple.audio. CLAP-wrapper then within GetParameterInfo calls get_value - this could be avoided I think.params.get_value #0: multiple main threads
CLAP-as-AUv2 in GarageBand 10.4.12
Possibly due to either get_value or state.load called from parallel 'main threads'
on_main_thread #0: multiple main threads
CLAP-as-AUv2 in Pluginval
activate #0: multiple main threads
CLAP-as-AUv2 in Pluginval
deactivate #0: multiple main threads
CLAP-as-AUv2 in Pluginval
These are AU only. Current theory: is CLAP-wrappers on_main_thread() callbacks really the main thread? If not, then that could be hogging the 'logical main thread' and therefore those errors arise? Not sure.
Discussion
All reactions