fix(core): dispose a clip's FX graph when it ends, not only when it is stopped

The `ended` listener spliced the source out of `_activeSources` and restored
the element's muted flag, but never disposed the FX handle — and the splice is
what made it unreachable, because `stopAll()` disposes by walking that same
array. So every clip that finished naturally leaked its graph for the rest of
the session.

The leak is not just memory. Each stale handle keeps a MutationObserver bound
to `data-fx-chain`/`data-automation`, so every later panel edit runs rebuild()
against a dead source: a fresh graph per abandoned clip, including a ~576 KB
reverb impulse and chorus/phaser oscillators that are started and never
stopped. Play a five-clip composition three times and fifteen observers answer
every subsequent knob turn.

At this point in the stack `attachElementFxChain` has no chainless early
return — it watches every audio clip, chain or not — so this leaked for all of
them, not only the ones carrying effects.

Disposal is skipped when the entry was already gone: `stopAll()` disposes its
own, and `stop()` is what fires this event.
This commit is contained in:
Vance Ingalls
2026-08-08 11:06:20 -07:00
parent 007c947221
commit 05d081c61d
2 changed files with 28 additions and 0 deletions
@@ -388,6 +388,19 @@ describe("WebAudioTransport", () => {
expect(transport.isActive()).toBe(false);
});
it("disposes the FX graph when a clip ends naturally", async () => {
// stopAll() disposes by walking _activeSources, and the splice above had
// already removed this entry — so the handle, its MutationObserver and any
// running LFO survived the clip for the rest of the session.
const { transport, mock, gen } = setupTransport(100);
await transport.schedulePlayback(mockEl, mockBuffer, 0, 0, 0, 1, gen);
mock.sourceNode._fireEnded();
expect(mock.sourceNode.disconnect).toHaveBeenCalled();
expect(mock.gainNode.disconnect).toHaveBeenCalled();
});
it("registers onended listener on the sourceNode", async () => {
const { transport, mock, gen } = setupTransport(100);
@@ -243,6 +243,21 @@ export class WebAudioTransport {
if (idx !== -1) {
this._activeSources.splice(idx, 1);
el.muted = priorMuted;
// The graph goes with it. Splicing alone left the FX handle alive and
// then UNREACHABLE — stopAll() disposes by walking this array, which
// the splice just emptied of this entry. Every clip that finished
// naturally leaked its MutationObserver for the session, and each one
// still answered later `data-fx-chain` edits by rebuilding a whole
// graph (impulse response, chorus/phaser oscillators started and never
// stopped) around a dead source. Not disposed when idx is -1: stopAll()
// has already done it, and `stop()` is what fired this event.
try {
sourceNode.disconnect();
fx?.dispose();
gainNode.disconnect();
} catch {
// Already torn down.
}
if (this._activeSources.length === 0) this._paused = true;
}
});