From f01438c3c60509df50ee98db38373a951f262b44 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 7 Nov 2026 21:45:26 +0000 Subject: [PATCH] fix(radio): Keep recovery from reporting aborted pending loads Recovery reloads call loadAttachedSource without bumping generation, so cancelLoad aborts the still-pending waitUntilReady and the same-generation AbortError was reported as an unexpected PLAY_ERROR. Track attachment epochs so a superseded pending load settles without onError, leave resume to the recovery reload, and keep real media errors visible. Fixes RADIO-27 https://avoidquest.sentry.io/issues/RADIO-27 Changelog: Recovery no longer reports a false playback failure when it cancels a stream that was still connecting. Co-authored-by: Francesco Castrovilli --- .../media-element-playback-source.test.ts | 92 +++++++++++++++++++ .../playback/media-element-playback-source.ts | 40 +++++++- 2 files changed, 127 insertions(+), 5 deletions(-) diff --git a/apps/radio/src/lib/audio/playback/media-element-playback-source.test.ts b/apps/radio/src/lib/audio/playback/media-element-playback-source.test.ts index 2c20a4642..08141e2e4 100644 --- a/apps/radio/src/lib/audio/playback/media-element-playback-source.test.ts +++ b/apps/radio/src/lib/audio/playback/media-element-playback-source.test.ts @@ -726,6 +726,98 @@ describe("MediaElementPlaybackSource native playback", () => { } ); + test("recovery reload canceling a pending load does not report AbortError", async () => { + jest.useFakeTimers(); + const browser = installBrowser(); + const onError = mock( + (_error: Error, _recoveryPending?: boolean) => undefined + ); + const onReady = mock(() => undefined); + + try { + const source = new MediaElementPlaybackSource(null, "native", { + onError, + onReady, + }); + const audio = browser.audio(); + const url = "https://radio.example/icecast-live.mp3"; + const loading = source.load({ + format: "progressive", + live: true, + src: url, + }); + const loadResult = loading.catch((error: unknown) => error); + const playing = source.play(); + const playResult = playing.catch((error: unknown) => error); + await flushMicrotasks(); + + audio.emit("waiting"); + jest.advanceTimersByTime(6000); + jest.advanceTimersByTime(0); + await flushMicrotasks(); + + expect(audio.loadSources).toEqual([url, url]); + // Recovery cancelLoad()-aborts the pending waitUntilReady. That must not + // surface as an unexpected PLAY_ERROR / onError AbortError. + expect(onError).not.toHaveBeenCalled(); + + audio.emit("canplay"); + await flushMicrotasks(); + expect(await loadResult).toBeUndefined(); + expect(await playResult).toBeUndefined(); + expect(onError).not.toHaveBeenCalled(); + expect(onReady).toHaveBeenCalled(); + expect(source.status).not.toBe("error"); + source.cleanup(); + } finally { + browser.restore(); + } + }); + + test("a real media error during a pending load still reports onError", async () => { + const browser = installBrowser(); + const onError = mock( + (_error: Error, _recoveryPending?: boolean) => undefined + ); + + try { + const source = new MediaElementPlaybackSource(null, "native", { + onError, + }); + const audio = browser.audio(); + const loading = source.load({ + format: "progressive", + live: true, + src: "https://radio.example/icecast-live.mp3", + }); + const loadResult = loading.catch((error: unknown) => error); + const playing = source.play(); + const playResult = playing.catch((error: unknown) => error); + await flushMicrotasks(); + + audio.error = { code: MediaError.MEDIA_ERR_NETWORK } as MediaError; + audio.emit("error"); + + expect(await loadResult).toHaveProperty( + "message", + "Audio stream failed to load" + ); + expect(await playResult).toHaveProperty( + "message", + "Audio stream failed to load" + ); + expect(onError).toHaveBeenCalledTimes(1); + expect(onError.mock.calls[0]?.[0]).toHaveProperty( + "message", + "Audio stream failed to load" + ); + expect(source.status).toBe("error"); + source.cleanup(); + } finally { + browser.restore(); + } + }); + test("reloads after connectivity returns without media progress", async () => { jest.useFakeTimers(); const browser = installBrowser(); diff --git a/apps/radio/src/lib/audio/playback/media-element-playback-source.ts b/apps/radio/src/lib/audio/playback/media-element-playback-source.ts index daf158e4b..b82b91dd1 100644 --- a/apps/radio/src/lib/audio/playback/media-element-playback-source.ts +++ b/apps/radio/src/lib/audio/playback/media-element-playback-source.ts @@ -11,6 +11,10 @@ import type { } from "./playback-source.js"; import type { StreamStatus } from "./types.js"; +function isAttachmentAbortError(error: unknown): boolean { + return error instanceof DOMException && error.name === "AbortError"; +} + export class MediaElementPlaybackSource implements PlaybackSource { private readonly callbacks: PlaybackSourceCallbacks; private readonly sourceId: string; @@ -22,6 +26,7 @@ export class MediaElementPlaybackSource implements PlaybackSource { private _status: StreamStatus = "idle"; private _isBuffering = false; private generation = 0; + private attachmentEpoch = 0; private playbackIntent = 0; private playbackRate = 1; private shouldResumeAfterLoad: boolean; @@ -219,6 +224,12 @@ export class MediaElementPlaybackSource implements PlaybackSource { throw error; } + // Recovery may cancel the initial attachment to start a same-generation + // reload. Leave resume to that reload instead of racing play() here. + if (this.recovery.isReloading) { + return; + } + const playResult = await playResultPromise; if (!playResult.ok) { const canRecoverPlayError = this.canRecoverPendingPlayError( @@ -389,14 +400,33 @@ export class MediaElementPlaybackSource implements PlaybackSource { input: PlaybackInput, generation: number ): Promise { - await this.attachment.load( - input, - () => generation === this.generation, - (event) => this.handleFatalHlsEvent(event) - ); + this.attachmentEpoch += 1; + const epoch = this.attachmentEpoch; + try { + await this.attachment.load( + input, + () => generation === this.generation && epoch === this.attachmentEpoch, + (event) => this.handleFatalHlsEvent(event) + ); + } catch (error) { + // Recovery reload calls loadAttachedSource again without bumping + // generation, which cancelLoad()-aborts this waiter. That AbortError is + // expected; the newer epoch owns readiness and error reporting. + if ( + isAttachmentAbortError(error) && + generation === this.generation && + epoch !== this.attachmentEpoch + ) { + return; + } + throw error; + } if (generation !== this.generation) { throw new DOMException("Load aborted", "AbortError"); } + if (epoch !== this.attachmentEpoch) { + return; + } this._status = this.audio.paused ? "buffering" : "streaming"; this.setBuffering(false); this.callbacks.onReady?.();