From 0bcc0e3fed1257c72e1cc79db4311cbe9df221e3 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Sat, 31 Oct 2026 14:05:03 +0000 Subject: [PATCH 1/2] fix: handle rejected play() in AudioPreviewPlayer (#22390) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary Fixes Sentry `NotSupportedError: The operation is not supported.` from the audio widget preview ([FRONT-3745](https://linear.app/comfyorg/issue/FRONT-3745), [FRONT-3747](https://linear.app/comfyorg/issue/FRONT-3747)). Safari rejects `HTMLMediaElement.play()` with that error when it cannot play a source. `AudioPreviewPlayer.togglePlayPause` called `void play()` and set `isPlaying = true` regardless. So the rejection went unhandled, the click did nothing, the button switched to pause, and the next click called `pause()` instead of retrying. Confirmed from the events: [CLOUD-FRONTEND-PROD-363](https://comfy-org.sentry.io/issues/7819622225/) (Safari 26.6.2, release 1.58.0) and [CLOUD-FRONTEND-PROD-2QY](https://comfy-org.sentry.io/issues/7794752354/) (release 1.56.3) both show `togglePlayPause` as the only first-party frame under `[native code] play`. Impact: 4 events, 3 users, 15 days. ## Changes - `play()` is awaited in `try/catch`. On failure the button returns to play, a warning is logged, and a localized error toast is shown. `AbortError` stays silent. - A `playAttempt` counter ignores late results from attempts superseded by pause, a source change, or unmount. - The toast has a stable id per player, so repeat clicks replace it. It clears on a successful retry, a source change, or unmount. - Clearing the source now pauses the element. Removing the `src` attribute alone leaves audio playing. - One `watch(modelValue)` replaces the previous `whenever`. Its `immediate` was a no-op because the template ref is null during setup. - `aria-pressed` on the toggle. - A failed source is reloaded before a retry, since `play()` keeps rejecting until `load()` runs. - The player follows `pause` and `error` events from the element, so media keys and mid-playback failures no longer leave the button stuck. - Clearing the source resets the shown time. - Failures go through `reportError` at warning level (`error_playing_audio`), so they stay measurable. ## Verification - Unit: 19 component tests. - Real WebKit via Playwright, isolated pattern page rather than the full app: old `void play()` logged the unhandled production message; the handled pattern stayed paused (screenshot below). ## Not in this PR - [FRONT-3746](https://linear.app/comfyorg/issue/FRONT-3746), fixed in #22451: the same error from `useWaveAudioPlayer` ([CLOUD-FRONTEND-PROD-31S](https://comfy-org.sentry.io/issues/7811786061/)). - Other alerts in this family come from the `comfyui-videohelpersuite` custom node, outside this repo. ## Screenshots ![WebKit: old void play() pattern produces unhandled NotSupportedError; fixed pattern handles it and stays paused](https://pub-1fd11710d4c8405b948c9edc4287a3f2.r2.dev/sessions/8cceed9166009a2c0adccdb2c79f80fc27d398c127c1c49ef1363ca5b3e7f8ee/pr-images/1793384742964-57e4d054-83dd-49e3-9a8b-f64432498181.png) ## Update: 208fc8d40b (@dante01yoon, comment 4430258389) Fixed the double `reportError`. `handleError` now claims the pending play attempt (`playAttempt++`), so the `play()` rejection that follows is ignored as stale. One click on a failed source now reports once, whichever arrives first: - error event first: `handleError` reports, and the stale catch exits. - rejection first: the catch reports and sets `isPlaying=false`, and the later error event hits the `!isPlaying` guard. There's a test for each order. The error-first test fails without the fix. Results: 24/24 component tests pass, lint/typecheck are clean, and the deep-reviewer verdict was PASS. ## Update: 577f69f712 (@dante01yoon, comments 4431351009/11/14/17) - Removed the JSDoc on the non-exported `toPlaybackError` and the three `//` comments in the tests. Behaviour and assertions are unchanged. - Renamed two tests so their titles carry the intent: "resets the shown elapsed time when the source is cleared mid-playback" and "dismisses only its own error toast when %s". - The older section comments (`// Refs`, `// Computed`, …) were already in the file before this PR, so I left them alone. - 24/24 component tests pass and lint is clean. --------- Co-authored-by: Glary-Bot --- .../audio/AudioPreviewPlayer.test.ts | 375 +++++++++++++++++- .../components/audio/AudioPreviewPlayer.vue | 98 ++++- 2 files changed, 454 insertions(+), 19 deletions(-) diff --git a/src/renderer/extensions/vueNodes/widgets/components/audio/AudioPreviewPlayer.test.ts b/src/renderer/extensions/vueNodes/widgets/components/audio/AudioPreviewPlayer.test.ts index 053c43d84f8..c57033b7642 100644 --- a/src/renderer/extensions/vueNodes/widgets/components/audio/AudioPreviewPlayer.test.ts +++ b/src/renderer/extensions/vueNodes/widgets/components/audio/AudioPreviewPlayer.test.ts @@ -1,9 +1,19 @@ -import { render, screen, waitFor, within } from '@testing-library/vue' +import { + fireEvent, + render, + screen, + waitFor, + within +} from '@testing-library/vue' +import { fromPartial } from '@total-typescript/shoehorn' import userEvent from '@testing-library/user-event' import { describe, expect, it, vi } from 'vitest' import { createI18n } from 'vue-i18n' +import { reportError } from '@comfyorg/telemetry/reportError' + import { useToast } from '@/components/ui/toast/toastStore' +import enMessages from '@/locales/en/main.json' with { type: 'json' } import AudioPreviewPlayer from '@/renderer/extensions/vueNodes/widgets/components/audio/AudioPreviewPlayer.vue' vi.mock(import('@/base/common/downloadUtil'), () => ({ @@ -13,9 +23,14 @@ vi.mock(import('@/base/common/downloadUtil'), () => ({ const i18n = createI18n({ legacy: false, locale: 'en', - messages: { en: {} } + messages: { + en: { g: { audioPlaybackFailed: enMessages.g.audioPlaybackFailed } } + } }) +const notSupported = () => + new DOMException('The operation is not supported.', 'NotSupportedError') + function renderPlayer(modelValue?: string) { return render(AudioPreviewPlayer, { props: { @@ -58,6 +73,362 @@ describe('AudioPreviewPlayer', () => { ).not.toBeChecked() }) + describe('play/pause button', () => { + it('stays paused and shows a toast when Safari rejects playback with NotSupportedError', async () => { + const play = vi + .spyOn(HTMLMediaElement.prototype, 'play') + .mockRejectedValue(notSupported()) + const pause = vi + .spyOn(HTMLMediaElement.prototype, 'pause') + .mockImplementation(() => {}) + const user = userEvent.setup() + + renderPlayer('http://example.com/audio.ogg') + const button = screen.getByRole('button', { name: 'g.playPause' }) + + await user.click(button) + await user.click(button) + + expect(play).toHaveBeenCalledTimes(2) + expect(pause).not.toHaveBeenCalled() + expect(reportError).toHaveBeenCalledWith( + expect.any(DOMException), + expect.objectContaining({ errorType: 'error_playing_audio' }) + ) + expect(useToast().toasts).toContainEqual( + expect.objectContaining({ + kind: 'error', + description: enMessages.g.audioPlaybackFailed + }) + ) + expect(useToast().toasts).toHaveLength(1) + }) + + it('reloads a failed source before retrying', async () => { + const play = vi + .spyOn(HTMLMediaElement.prototype, 'play') + .mockResolvedValue(undefined) + const load = vi + .spyOn(HTMLMediaElement.prototype, 'load') + .mockImplementation(() => {}) + const user = userEvent.setup() + + renderPlayer('http://example.com/audio.ogg') + vi.spyOn(HTMLMediaElement.prototype, 'error', 'get').mockReturnValue( + fromPartial({ code: 4 }) + ) + + await user.click(screen.getByRole('button', { name: 'g.playPause' })) + + expect(load).toHaveBeenCalledTimes(1) + expect(load.mock.invocationCallOrder[0]).toBeLessThan( + play.mock.invocationCallOrder[0] + ) + }) + + it('follows a pause that came from outside the player', async () => { + vi.spyOn(HTMLMediaElement.prototype, 'play').mockResolvedValue(undefined) + vi.spyOn(HTMLMediaElement.prototype, 'paused', 'get').mockReturnValue( + true + ) + const user = userEvent.setup() + + renderPlayer('http://example.com/audio.mp3') + const button = screen.getByRole('button', { name: 'g.playPause' }) + await user.click(button) + expect(button).toHaveAttribute('aria-pressed', 'true') + + await fireEvent(screen.getByTestId('audio-preview'), new Event('pause')) + + expect(button).toHaveAttribute('aria-pressed', 'false') + }) + + it('reports the native media diagnostics when playback fails after it started', async () => { + vi.spyOn(HTMLMediaElement.prototype, 'play').mockResolvedValue(undefined) + const user = userEvent.setup() + + renderPlayer('http://example.com/audio.mp3') + const button = screen.getByRole('button', { name: 'g.playPause' }) + await user.click(button) + vi.spyOn(HTMLMediaElement.prototype, 'error', 'get').mockReturnValue( + fromPartial({ + code: 4, + message: 'DEMUXER_ERROR_COULD_NOT_OPEN' + }) + ) + + await fireEvent.error(screen.getByTestId('audio-preview')) + + expect(button).toHaveAttribute('aria-pressed', 'false') + expect(useToast().toasts).toHaveLength(1) + const [reported] = vi.mocked(reportError).mock.calls[0] + expect(reported).toBeInstanceOf(Error) + expect(reportError).toHaveBeenCalledWith( + expect.objectContaining({ + message: + 'Audio playback failed (MediaError 4): DEMUXER_ERROR_COULD_NOT_OPEN' + }), + expect.objectContaining({ errorType: 'error_playing_audio' }) + ) + }) + + it('dismisses the error toast when a retry is paused', async () => { + const play = vi + .spyOn(HTMLMediaElement.prototype, 'play') + .mockRejectedValueOnce(notSupported()) + .mockImplementationOnce(() => new Promise(() => {})) + vi.spyOn(HTMLMediaElement.prototype, 'pause').mockImplementation(() => {}) + const user = userEvent.setup() + + renderPlayer('http://example.com/audio.mp3') + const button = screen.getByRole('button', { name: 'g.playPause' }) + await user.click(button) + expect(useToast().toasts).toHaveLength(1) + + await user.click(button) + await user.click(button) + + expect(play).toHaveBeenCalledTimes(2) + expect(useToast().toasts).toEqual([]) + }) + + it('resets the shown elapsed time when the source is cleared mid-playback', async () => { + vi.spyOn(HTMLMediaElement.prototype, 'duration', 'get').mockReturnValue( + 90 + ) + vi.spyOn( + HTMLMediaElement.prototype, + 'currentTime', + 'get' + ).mockReturnValue(27) + + const { rerender } = renderPlayer('http://example.com/audio.mp3') + await fireEvent.loadedMetadata(screen.getByTestId('audio-preview')) + await fireEvent.timeUpdate(screen.getByTestId('audio-preview')) + expect(screen.getByText('0:27 / 1:30')).toBeInTheDocument() + + await rerender({ modelValue: undefined }) + + expect(screen.getByText('0:00 / 0:00')).toBeInTheDocument() + }) + + it.for([ + ['the source is replaced', (r) => r.rerender({ modelValue: 'b.mp3' })], + ['the source is cleared', (r) => r.rerender({ modelValue: undefined })], + ['the player unmounts', (r) => r.unmount()] + ] satisfies [string, (r: ReturnType) => unknown][])( + 'dismisses only its own error toast when %s', + async ([, transition]) => { + vi.spyOn(HTMLMediaElement.prototype, 'play').mockRejectedValue( + notSupported() + ) + const user = userEvent.setup() + const unrelatedId = useToast().info('Unrelated notice') + const rendered = renderPlayer('http://example.com/audio.mp3') + await user.click(screen.getByRole('button', { name: 'g.playPause' })) + expect(useToast().toasts).toHaveLength(2) + + await transition(rendered) + + expect(useToast().toasts).toEqual([ + expect.objectContaining({ id: unrelatedId }) + ]) + } + ) + + it('dismisses the playback error toast after a successful retry', async () => { + vi.spyOn(HTMLMediaElement.prototype, 'play') + .mockRejectedValueOnce(notSupported()) + .mockResolvedValueOnce(undefined) + const user = userEvent.setup() + + renderPlayer('http://example.com/audio.mp3') + const button = screen.getByRole('button', { name: 'g.playPause' }) + + await user.click(button) + expect(useToast().toasts).toHaveLength(1) + + await user.click(button) + expect(useToast().toasts).toEqual([]) + }) + + it.for([ + ['replaced', 'http://example.com/other.mp3'], + ['cleared', undefined] + ] as const)( + 'ignores a pending rejection after the source is %s', + async ([, nextSource]) => { + const attempts: Array<(reason: unknown) => void> = [] + vi.spyOn(HTMLMediaElement.prototype, 'play').mockImplementation( + () => + new Promise((_resolve, reject) => { + attempts.push(reject) + }) + ) + const user = userEvent.setup() + + const { rerender } = renderPlayer('http://example.com/audio.mp3') + await user.click(screen.getByRole('button', { name: 'g.playPause' })) + await rerender({ modelValue: nextSource }) + attempts[0](notSupported()) + await Promise.resolve() + + expect(useToast().toasts).toEqual([]) + expect( + screen.getByRole('button', { name: 'g.playPause' }) + ).toHaveAttribute('aria-pressed', 'false') + } + ) + + it('pauses the element when the source is cleared mid-playback', async () => { + vi.spyOn(HTMLMediaElement.prototype, 'play').mockResolvedValue(undefined) + const pause = vi + .spyOn(HTMLMediaElement.prototype, 'pause') + .mockImplementation(() => {}) + const user = userEvent.setup() + + const { rerender } = renderPlayer('http://example.com/audio.mp3') + const button = screen.getByRole('button', { name: 'g.playPause' }) + + await user.click(button) + expect(button).toHaveAttribute('aria-pressed', 'true') + pause.mockClear() + + await rerender({ modelValue: undefined }) + + expect(pause).toHaveBeenCalledTimes(1) + expect(button).toHaveAttribute('aria-pressed', 'false') + }) + + it('pauses the still-attached element when the default player hides on clear', async () => { + vi.spyOn(HTMLMediaElement.prototype, 'play').mockResolvedValue(undefined) + const connectedAtPause: boolean[] = [] + vi.spyOn(HTMLMediaElement.prototype, 'pause').mockImplementation( + function (this: HTMLMediaElement) { + connectedAtPause.push(this.isConnected) + } + ) + const user = userEvent.setup() + + const { rerender } = render(AudioPreviewPlayer, { + props: { modelValue: 'http://example.com/audio.mp3' }, + global: { plugins: [i18n] } + }) + await user.click(screen.getByRole('button', { name: 'g.playPause' })) + connectedAtPause.length = 0 + + await rerender({ modelValue: undefined }) + + expect(connectedAtPause).toEqual([true]) + expect( + screen.queryByRole('button', { name: 'g.playPause' }) + ).not.toBeInTheDocument() + }) + + it('ignores a pending rejection after unmount', async () => { + const attempts: Array<(reason: unknown) => void> = [] + vi.spyOn(HTMLMediaElement.prototype, 'play').mockImplementation( + () => + new Promise((_resolve, reject) => { + attempts.push(reject) + }) + ) + const user = userEvent.setup() + + const { unmount } = renderPlayer('http://example.com/audio.mp3') + await user.click(screen.getByRole('button', { name: 'g.playPause' })) + unmount() + attempts[0](notSupported()) + await Promise.resolve() + + expect(useToast().toasts).toEqual([]) + }) + + it('reports once when the source fails while a play attempt is pending', async () => { + vi.spyOn(HTMLMediaElement.prototype, 'play').mockImplementation( + function (this: HTMLMediaElement) { + const rejection = Promise.reject(notSupported()) + this.dispatchEvent(new Event('error')) + return rejection + } + ) + const user = userEvent.setup() + + renderPlayer('http://example.com/audio.ogg') + const button = screen.getByRole('button', { name: 'g.playPause' }) + await user.click(button) + + expect(reportError).toHaveBeenCalledTimes(1) + expect(useToast().toasts).toHaveLength(1) + expect(button).toHaveAttribute('aria-pressed', 'false') + }) + + it('reports once when the source error follows a handled rejection', async () => { + const elements: HTMLMediaElement[] = [] + vi.spyOn(HTMLMediaElement.prototype, 'play').mockImplementation( + function (this: HTMLMediaElement) { + elements.push(this) + return Promise.reject(notSupported()) + } + ) + const user = userEvent.setup() + + renderPlayer('http://example.com/audio.ogg') + await user.click(screen.getByRole('button', { name: 'g.playPause' })) + elements[0].dispatchEvent(new Event('error')) + + expect(reportError).toHaveBeenCalledTimes(1) + expect(useToast().toasts).toHaveLength(1) + }) + + it('stays silent when the current play attempt is aborted', async () => { + const play = vi + .spyOn(HTMLMediaElement.prototype, 'play') + .mockRejectedValue(new DOMException('Aborted', 'AbortError')) + const user = userEvent.setup() + + renderPlayer('http://example.com/audio.mp3') + const button = screen.getByRole('button', { name: 'g.playPause' }) + + await user.click(button) + await user.click(button) + + expect(play).toHaveBeenCalledTimes(2) + expect(reportError).not.toHaveBeenCalled() + expect(useToast().toasts).toEqual([]) + }) + + it('ignores a rejection from a superseded play attempt', async () => { + const attempts: Array<(reason: unknown) => void> = [] + const play = vi + .spyOn(HTMLMediaElement.prototype, 'play') + .mockImplementation( + () => + new Promise((_resolve, reject) => { + attempts.push(reject) + }) + ) + const pause = vi + .spyOn(HTMLMediaElement.prototype, 'pause') + .mockImplementation(() => {}) + const user = userEvent.setup() + + renderPlayer('http://example.com/audio.mp3') + const button = screen.getByRole('button', { name: 'g.playPause' }) + + await user.click(button) + await user.click(button) + await user.click(button) + attempts[0](new DOMException('Interrupted by pause', 'AbortError')) + await user.click(button) + + expect(play).toHaveBeenCalledTimes(2) + expect(pause).toHaveBeenCalledTimes(2) + expect(reportError).not.toHaveBeenCalled() + expect(useToast().toasts).toEqual([]) + }) + }) + describe('download button', () => { it('shows download button when audio is loaded', () => { renderPlayer('http://example.com/audio.mp3') diff --git a/src/renderer/extensions/vueNodes/widgets/components/audio/AudioPreviewPlayer.vue b/src/renderer/extensions/vueNodes/widgets/components/audio/AudioPreviewPlayer.vue index 37e0bcf52f3..48b655ce92b 100644 --- a/src/renderer/extensions/vueNodes/widgets/components/audio/AudioPreviewPlayer.vue +++ b/src/renderer/extensions/vueNodes/widgets/components/audio/AudioPreviewPlayer.vue @@ -8,9 +8,12 @@