From 7a7077658fd67bd48f1ea4f887001267be598e57 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 7 Nov 2026 21:41:19 +0000 Subject: [PATCH] fix(radio): Keep recovery reloads from raising false play errors Recovery reattaches media through loadAttachedSource without load(), so an in-flight play() could still reject with Chrome's AbortError when the element loaded again. Supersede that play intent without clearing resume, and route recovery resume through the same ownership check. Fixes RADIO-24 https://avoidquest.sentry.io/issues/RADIO-24 Changelog: Recovery no longer reports a false playback failure when it reloads a stream that was already trying to play. Co-authored-by: Francesco Castrovilli --- .../media-element-playback-source.test.ts | 34 +++++++++++++++++++ .../playback/media-element-playback-source.ts | 19 ++++++++--- 2 files changed, 49 insertions(+), 4 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..f3c4bdc57 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 @@ -240,6 +240,40 @@ describe("MediaElementPlaybackSource native playback", () => { } }); + test("a recovery reload does not reject an in-flight play for the same resume intent", async () => { + jest.useFakeTimers(); + const browser = installBrowser(); + const source = new MediaElementPlaybackSource(null, "native"); + try { + const audio = browser.audio(); + await startNativeStream(source, audio, "https://radio.example/live.mp3"); + source.pause(); + const pending = Promise.withResolvers(); + audio.play = () => { + audio.paused = false; + return pending.promise; + }; + const result = source.play().catch((error: unknown) => error); + audio.emit("waiting"); + jest.advanceTimersByTime(6000); + jest.advanceTimersByTime(0); + await flushMicrotasks(); + // Recovery reattaches through loadAttachedSource without going through + // load(), so the browser rejects the earlier play() with this AbortError. + pending.reject( + new DOMException( + "The play() request was interrupted by a new load request.", + "AbortError" + ) + ); + expect(await result).toBeUndefined(); + expect(source.status).not.toBe("error"); + } finally { + source.cleanup(); + browser.restore(); + } + }); + test.each(["AbortError", "NotAllowedError", "NotSupportedError"])( "an active play still reports %s", async (name) => { 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..dbbca0e73 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 @@ -70,11 +70,18 @@ export class MediaElementPlaybackSource implements PlaybackSource { this.setBuffering(false); this.callbacks.onError?.(error, recoveryPending); }, - reload: (input, generation) => this.loadAttachedSource(input, generation), + reload: (input, generation) => { + // Recovery reattaches without load(), so it must still retire any + // in-flight play() whose browser promise the reattach will abort. + // Keep shouldResumeAfterLoad so resume can continue afterward. + this.supersedeInFlightPlay(); + return this.loadAttachedSource(input, generation); + }, resume: async () => { this.audio.autoplay = true; + this.shouldResumeAfterLoad = true; if (this.audio.paused) { - await this.audio.play(); + await this.playForIntent(this.playbackIntent); } }, setState: (status, isBuffering) => { @@ -435,6 +442,10 @@ export class MediaElementPlaybackSource implements PlaybackSource { private cancelPendingPlaybackIntent(): void { this.shouldResumeAfterLoad = false; this.audio.autoplay = false; + this.supersedeInFlightPlay(); + } + + private supersedeInFlightPlay(): void { this.playbackIntent += 1; } @@ -442,8 +453,8 @@ export class MediaElementPlaybackSource implements PlaybackSource { try { await this.audio.play(); } catch (error) { - // A later pause, stop, load or play owns the element now. The browser - // can reject the old play promise after that action has completed. + // A later pause, stop, load, play or recovery reload owns the element + // now. The browser can reject the old play promise after that action. if (!this.isPlaybackIntentCanceled(playbackIntent)) { throw error; }