prepareToPlay and processBlock thread-safety

OK: setLatencySamples() in preapreToPlay() is the cause.
With setLatency(): Host calls prepareToPlay(), in which latency is set, which causes the VST3 to restart in a ComponentRestarter::handleAsyncUpdate().
I’m guessing this causes prepareToPlay a second time while the audio side has already started callbacks.

I see here that componentRestarter originally disallowed restarting during prepareToPlay… but a fix was made to allow it.
I wonder if it was never noticed that this race condition was the reason it was originally disallowed.

But I’m not sure how else we are supposed to report latency. We must do it in samples, and the sample rate is not known until prepareToPlay…
Storing it and setting latency in the first audio callback can’t be safe/reliable… I’d think most hosts would want to know the latency value before delivering the first buffer.

FTR:
Here’s what happens in a DOP instance where it’s easy to get an overlap in the callbacks (though I saw it in a realtime instance as well). The timing varies massively.

prepareToPlay on thread: 0x106704580
prepareToPlay ENDS
processBlock on thread: 0x16e047000
prepareToPlay on thread: 0x106704580
processBlock ENDS
processBlock on thread: 0x16e047000
processBlock ENDS
processBlock on thread: 0x16e047000
processBlock ENDS

… Many more processBlock() calls

processBlock on thread: 0x16e047000
processBlock ENDS
prepareToPlay ENDS

Call stack for prepareToPlay is the same every time:

|#0|0x000000017cdd27d4 in MYPLUGINAudioProcessor::prepareToPlay(double, int) at /Users/Me/MYPLUGIN/Source/PluginProcessor.cpp:544|
|---|---|
|#1|0x000000017ccd54cc in juce::JuceVST3Component::preparePlugin(double, int, juce::JuceVST3Component::CallPrepareToPlay) at /Users/Me/MYPLUGIN/_SDKs/juce/modules/juce_audio_plugin_client/juce_audio_plugin_client_VST3.cpp:3750|
|#2|0x000000017ccc11c8 in juce::JuceVST3Component::setActive(unsigned char) at /Users/Me/MYPLUGIN/_SDKs/juce/modules/juce_audio_plugin_client/juce_audio_plugin_client_VST3.cpp:2593|
|#3|0x0000000101b89000 in ___lldb_unnamed_symbol94968$$Nuendo 12 ()|
1 Like