Skip to content

Fix racing condition in TestSceneAudioMixer.cs (reopened)#6718

Open
ShibuyaCyana wants to merge 3 commits intoppy:masterfrom
ShibuyaCyana:mixer_init_fix
Open

Fix racing condition in TestSceneAudioMixer.cs (reopened)#6718
ShibuyaCyana wants to merge 3 commits intoppy:masterfrom
ShibuyaCyana:mixer_init_fix

Conversation

@ShibuyaCyana
Copy link
Copy Markdown

This is a reopening of #6715 with a correct branch from my fork.

The Symptom

When you smash on the AudioMixer test scene button, it should play the sample immediately, but most of the time it doesn't play any sound:

before.mp4

Since dragging the pink speaker onto mixers are working fine, I guess this is why we've been ignoring this.

The Cause

There's a race condition during test scene initialization.

  • AudioPlayingDrawable.load() schedules a mixer.Add(channel) in Update thread
  • DrawableAudioMixer.load() schedules a createMixer in another background thread
  • They both schedule things to run in the audio thread eventually, but queuing sequence may different
  • Channel might be added to the mixer before mixer creation complete, which results in an early return in BassAudioMixer.ChannelPlay (now renamed to AddToBassMixAndPlay).

The Fix

Fixing that symptom solely is just a one-liner:

// BassAudioMixer.AddInternal
if (Handle == 0 || bassChannel.Handle == 0)
{
    // Do not return early immediately
    if (!activeChannels.Contains(bassChannel))
        activeChannels.Add(bassChannel);
    return;
}

But I took a more rigorous approach: to track the channels adding before mixer creation complete as pending, and retry them in UpdateState.

This is what it should have been (after the fix):

after.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants