Oscillators: Fixed the data race on the static juce::Random shared by start() - #406
Merged
Merged
Conversation
…very start() call (fixes #400) Oscillator::start() and MultiVoiceOscillator::start() drew their random start phase from a function-local static juce::Random, so voices started at the same time on different audio threads (e.g. two 4OSC instances on separate tracks) raced on its seed. Each oscillator now owns its own juce::Random, which seeds itself on construction, so behaviour is unchanged but nothing is shared between instances. Adds a doctest case that starts oscillators concurrently from several threads (caught by the ThreadSanitizer CI job) and a check that start() draws a fresh phase per call and per instance.
1 task
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #406 +/- ##
===========================================
+ Coverage 58.71% 58.74% +0.03%
===========================================
Files 563 564 +1
Lines 78823 78879 +56
Branches 12325 12332 +7
===========================================
+ Hits 46279 46337 +58
+ Misses 32544 32542 -2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Summary
Oscillator::start()andMultiVoiceOscillator::start()each drew their random start phase from a function-localstatic juce::Random. Each oscillator now owns ajuce::Randommember instead, so starting a note only touches state belonging to that instance. Behaviour is unchanged: every note still gets a random start phase, andjuce::Random's default constructor seeds itself, so instances don't share a sequence.Root cause
The static generator was shared by every oscillator in the process and is entered from the audio thread:
FourOscVoice::noteStarted()callsMultiVoiceOscillator::start()on each of its four oscillators from withinrenderNextBlock, on new notes and on retrigger. With more than one audio worker thread (the default is the CPU count), two 4OSC instances on different tracks starting notes at the same time race onjuce::Random's seed, which is not thread safe.Oscillator::start()is not called from within the engine but is public API and had the same construction, so it gets the same fix.Cost is one
juce::Random(16 bytes) perOscillatorand perMultiVoiceOscillator.Regression test
New
tracktion_Oscillators.test.cpp(enabled viaENGINE_UNIT_TESTS_OSCILLATORS):MultiVoiceOscillatorand anOscillatorand callstart()repeatedly at the same time. Nothing should be shared between them. The race is silent without ThreadSanitizer, so this case is aimed at the TSan CI job. Verified locally with a-fsanitize=threadDebug build: before the fix TSan reports 4 data races injuce::Random::nextInt()via bothstart()overloads and the run exits 134; after the fix the run is clean.start()calls on one instance, andstart()on freshly constructed instances, produce different first samples. This guards against regressing to a fixed or default-seeded generator (e.g. reusing thestd::default_random_enginethe noise path already owns, which would give every instance the same phase sequence).Full Debug
TestRunnerrun locally: all JUCE unit tests pass; the only doctest failures are two pre-existingClipLauncheraudio-clip cases that fail identically on a develop baseline build on this machine while develop CI is green.Fixes #400