Skip to content

Fix static sound disconnect edge case #16846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2025

Conversation

docEdub
Copy link
Contributor

@docEdub docEdub commented Jul 3, 2025

In rare cases when lots of static sounds are playing, it's possible for static sound instances to have their _onEnded handler called by the underlying AudioBufferSourceNode after the sound has been disposed. This causes the static sound instance _disconnect call to occur twice, which throws an error on the 2nd call since it is already disconnected.

This change adds an _isConnected flag to static sound instances which skips the 2nd call to _disconnect if it is already disconnected.

In rare cases when lots of static sounds are playing, it's possible for static sound instances to have their `_onEnded` handler called by the underlying AudioBufferSourceNode *after* the sound has been disposed. This causes the static sound instance `_disconnect` call to occur twice, which throws an error on the 2nd call since it is already disconnected.

This change adds an `_isConnected` flag to static sound instances which skips the 2nd call to `_disconnect` if it is already disconnected.
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR prevents a static sound instance from attempting to disconnect twice by introducing an _isConnected flag and guarding the _disconnect call.

  • Added an _isConnected flag to _WebAudioStaticSoundInstance
  • Set/reset the flag when connecting/disconnecting
  • Updated the _onEnded handler to skip a second disconnect
Comments suppressed due to low confidence (4)

packages/dev/core/src/AudioV2/webAudio/webAudioStaticSound.ts:310

  • [nitpick] The flag name _isConnected is ambiguous—consider renaming it to something like _hasActiveConnection or _isConnectedToNode to make its purpose clearer.
    private _isConnected: boolean = false;

packages/dev/core/src/AudioV2/webAudio/webAudioStaticSound.ts:310

  • Add a comment explaining why _isConnected is needed to guard against double-disconnect errors when the AudioBufferSourceNode triggers its onended after disposal.
    private _isConnected: boolean = false;

packages/dev/core/src/AudioV2/webAudio/webAudioStaticSound.ts:521

  • Add a unit test that simulates the onended event firing after dispose to verify that _disconnect is called only once and no error is thrown.
        if (this._isConnected && !this._disconnect(this._sound)) {

packages/dev/core/src/AudioV2/webAudio/webAudioStaticSound.ts:488

  • The flag is only set when connecting to a _WebAudioStaticSound node; if the instance connects to other node types, _isConnected remains false and may skip needed disconnects. Consider setting the flag for all connect calls or scoping the logic more precisely.
            this._isConnected = true;

@bjsplat
Copy link
Collaborator

bjsplat commented Jul 3, 2025

Please make sure to label your PR with "bug", "new feature" or "breaking change" label(s).
To prevent this PR from going to the changelog marked it with the "skip changelog" label.

@docEdub docEdub enabled auto-merge (squash) July 3, 2025 15:49
@bjsplat
Copy link
Collaborator

bjsplat commented Jul 3, 2025

@bjsplat
Copy link
Collaborator

bjsplat commented Jul 3, 2025

@bjsplat
Copy link
Collaborator

bjsplat commented Jul 3, 2025

@docEdub docEdub merged commit 15100ac into BabylonJS:master Jul 3, 2025
16 of 18 checks passed
@bjsplat
Copy link
Collaborator

bjsplat commented Jul 3, 2025

@docEdub docEdub deleted the 260703-fix-disconnect-edge-case branch July 3, 2025 19:40
VicenteCartas pushed a commit to VicenteCartas/Babylon.js that referenced this pull request Aug 7, 2025
In rare cases when lots of static sounds are playing, it's possible for
static sound instances to have their `_onEnded` handler called by the
underlying AudioBufferSourceNode *after* the sound has been disposed.
This causes the static sound instance `_disconnect` call to occur twice,
which throws an error on the 2nd call since it is already disconnected.

This change adds an `_isConnected` flag to static sound instances which
skips the 2nd call to `_disconnect` if it is already disconnected.
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.

3 participants