[PR #5890] [MERGED] fix(tts): stop Safari's blocked-autoplay retry loop from freezing the tab #6981

Closed
opened 2026-10-23 04:15:27 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/5890
Author: @kolacheee
Created: 9/6/2026
Status: ✅ Merged
Merged: 9/6/2026
Merged by: @kolacheee

Base: staging ← Head: fix/5889-tts-safari-play-loop


📝 Commits (5)

  • 15c9914 fix(tts): stop Safari's blocked-autoplay retry loop from freezing the tab (#5889)
  • cbee321 Merge branch 'staging' into fix/5889-tts-safari-play-loop
  • 575cef4 fix(tts): settle real errors before releasing parked playback (CodeRabbit)
  • 23b4a91 fix(tts): surface the blocked state from resume and restart too (CodeRabbit)
  • 2abe86a test(tts): assert the blocked state per playback path, not by count (CodeRabbit)

📊 Changes

10 files changed (+492 additions, -29 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 packages/client/package.json (+1 -1)
📝 packages/client/src/components/chat/ChatArea.tsx (+16 -0)
📝 packages/client/src/components/chat/ChatMessage.tsx (+4 -3)
📝 packages/client/src/components/panels/settings/TTSConfigCard.tsx (+5 -5)
📝 packages/client/src/lib/tts-audio-cache.ts (+56 -9)
📝 packages/client/src/lib/tts-service.ts (+108 -11)
📝 packages/client/src/localization/locales/en.json (+1 -0)
➕ scripts/regressions/tsconfig.client-lanes.json (+15 -0)
➕ scripts/regressions/tts-safari-play-guard.regression.ts (+285 -0)

📄 Description

Linked issue

Closes #5889

Why this change

A user reported "TTS freezes my whole Marinara instance" — Safari on macOS and iOS, any voice, autoplay or manual, cured only by disabling TTS. The investigation (recorded on the issue) exonerated the server (/speak is a stateless binary proxy; audio never enters chat data) and pinned the freeze on the client: Safari rejects audio.play() with NotAllowedError when there's no live user activation, and playWhenAvailable treated that as "wait until the tab is visible and focused" — which it already was — so it retried with zero backoff, forever: a promise and a DOMException per iteration until the tab froze and WebKit killed it. Manual play hits the same wall because the click's activation expires while the synthesis fetch is awaited.

What changed

  • Blocked playback parks instead of spinning. On NotAllowedError in a visible tab, playWhenAvailable waits on a one-shot user-gesture listener (pointerdown/keydown/touchend); the retried play() lands inside that gesture's transient activation, which is exactly what makes Safari allow it. One mechanism covers autoplay and manual play — the fresh-Audio-per-chunk architecture rules out per-element gesture priming, so this is deliberately not the play-then-swap-src pattern.
  • A new "blocked" TTS state with honest UX: ChatArea shows a localized "tap anywhere or press any key to play" toast (id-deduped, dismissed the moment playback resumes), ChatMessage counts blocked as busy, and the settings preview button treats blocked as stoppable.
  • Bounded everything: 20-attempt cap that surfaces the original policy error; the hidden-tab path keeps its old visibility wait plus a 250 ms floor; non-policy errors rethrow immediately; a decode error arriving while parked aborts the controller so the gesture listeners are released and no future keystroke retries a dead element on a revoked object URL.
  • iOS memory relief: the in-memory audio cache — the only tier on iOS, where the persistent tier is deliberately disabled — gains a 64 MB byte cap with LRU eviction and reference-aware accounting (one Blob under alias keys books its size once). This addresses the reporter's "Safari memory shot way up" history independently of the spin.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

  • New tts-safari-play-guard lane drives the real exported playWhenAvailable with stubbed globals. The central assertion is the bug's own signature: a blocked visible tab makes exactly one play() call in a 300 ms window (the old code: thousands), a gesture earns exactly one retry, the cap ends a hopeless loop at 20 with the policy error surfaced, non-policy errors never retry, and the byte/alias cache accounting is exact.
  • Client lanes need DOM lib types, which conflict with the server lanes' node types, so they get their own tsconfig chained into client lint — resolving @marinara-engine/shared from source, because fresh-checkout CI lints before anything builds shared dist/. Verified by simulation: client lint passes with packages/shared/dist renamed away.
  • An adversarial multi-agent review ran before opening; all five confirmed findings are folded in (the parked-listener release, alias double-counting, the fresh-checkout CI break, toast dismissal, and the settings preview). Notably refuted by the review: per-chunk re-blocking on strict Safari, and the cap exhausting during normal backgrounded listening.
  • Real-Safari verification still wanted: I cannot run Safari from this environment. The reporter (or anyone on macOS/iOS with restrictive autoplay settings) should confirm: TTS on a blocked tab shows the toast instead of freezing, one tap resumes audio, and the stop button works while blocked.

Docs and release impact

  • No docs changes needed (CHANGELOG entry included)
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation)
  • Version/release files updated (only if this PR includes a version bump)

UI evidence (if applicable)

The visible change is the tap-to-play toast and the preview button's stop state while blocked — both need a real Safari session to demonstrate (see manual verification).

Summary by CodeRabbit

  • Bug Fixes

    • Safari text-to-speech playback no longer repeatedly retries blocked audio or freezes the tab.
    • Blocked playback now waits for a tap, key press, or touch interaction before retrying.
    • Added a notification when playback is blocked.
    • TTS controls now reflect blocked playback and prevent conflicting actions.
    • Limited iPhone and iPad audio memory usage to improve stability, with improved cache eviction.
  • Tests

    • Added regression coverage for blocked playback, retry limits, user-gesture recovery, and audio cache limits.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/5890 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 9/6/2026 **Status:** ✅ Merged **Merged:** 9/6/2026 **Merged by:** [@kolacheee](https://github.com/kolacheee) **Base:** `staging` ← **Head:** `fix/5889-tts-safari-play-loop` --- ### 📝 Commits (5) - [`15c9914`](https://github.com/Pasta-Devs/Marinara-Engine/commit/15c99142199decb498e6bc9e1dff775f7c30664d) fix(tts): stop Safari's blocked-autoplay retry loop from freezing the tab (#5889) - [`cbee321`](https://github.com/Pasta-Devs/Marinara-Engine/commit/cbee32129e9971d51c8fa266fc9e892be5b43bc6) Merge branch 'staging' into fix/5889-tts-safari-play-loop - [`575cef4`](https://github.com/Pasta-Devs/Marinara-Engine/commit/575cef425f47105e3ead3c92ab6382742e9bb810) fix(tts): settle real errors before releasing parked playback (CodeRabbit) - [`23b4a91`](https://github.com/Pasta-Devs/Marinara-Engine/commit/23b4a91d206ccbab6b2f8016e7071c69a6c9e12e) fix(tts): surface the blocked state from resume and restart too (CodeRabbit) - [`2abe86a`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2abe86ae7bd6d444b19af0bc1d179b2d7b9e70ab) test(tts): assert the blocked state per playback path, not by count (CodeRabbit) ### 📊 Changes **10 files changed** (+492 additions, -29 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `packages/client/package.json` (+1 -1) 📝 `packages/client/src/components/chat/ChatArea.tsx` (+16 -0) 📝 `packages/client/src/components/chat/ChatMessage.tsx` (+4 -3) 📝 `packages/client/src/components/panels/settings/TTSConfigCard.tsx` (+5 -5) 📝 `packages/client/src/lib/tts-audio-cache.ts` (+56 -9) 📝 `packages/client/src/lib/tts-service.ts` (+108 -11) 📝 `packages/client/src/localization/locales/en.json` (+1 -0) ➕ `scripts/regressions/tsconfig.client-lanes.json` (+15 -0) ➕ `scripts/regressions/tts-safari-play-guard.regression.ts` (+285 -0) </details> ### 📄 Description ## Linked issue Closes #5889 ## Why this change A user reported "TTS freezes my whole Marinara instance" — Safari on macOS and iOS, any voice, autoplay or manual, cured only by disabling TTS. The investigation (recorded on the issue) exonerated the server (`/speak` is a stateless binary proxy; audio never enters chat data) and pinned the freeze on the client: Safari rejects `audio.play()` with `NotAllowedError` when there's no live user activation, and `playWhenAvailable` treated that as "wait until the tab is visible and focused" — which it already was — so it retried **with zero backoff, forever**: a promise and a DOMException per iteration until the tab froze and WebKit killed it. Manual play hits the same wall because the click's activation expires while the synthesis fetch is awaited. ## What changed - **Blocked playback parks instead of spinning.** On `NotAllowedError` in a visible tab, `playWhenAvailable` waits on a one-shot user-gesture listener (pointerdown/keydown/touchend); the retried `play()` lands inside that gesture's transient activation, which is exactly what makes Safari allow it. One mechanism covers autoplay *and* manual play — the fresh-`Audio`-per-chunk architecture rules out per-element gesture priming, so this is deliberately not the play-then-swap-src pattern. - **A new `"blocked"` TTS state with honest UX**: ChatArea shows a localized "tap anywhere or press any key to play" toast (id-deduped, dismissed the moment playback resumes), ChatMessage counts blocked as busy, and the settings preview button treats blocked as stoppable. - **Bounded everything**: 20-attempt cap that surfaces the original policy error; the hidden-tab path keeps its old visibility wait plus a 250 ms floor; non-policy errors rethrow immediately; a decode error arriving *while parked* aborts the controller so the gesture listeners are released and no future keystroke retries a dead element on a revoked object URL. - **iOS memory relief**: the in-memory audio cache — the *only* tier on iOS, where the persistent tier is deliberately disabled — gains a 64 MB byte cap with LRU eviction and reference-aware accounting (one Blob under alias keys books its size once). This addresses the reporter's "Safari memory shot way up" history independently of the spin. ## Validation - [ ] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [ ] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [ ] Above manual verification completed (describe below) - [ ] Read and followed `CONTRIBUTING.md` ### Manual verification notes - New `tts-safari-play-guard` lane drives the **real exported `playWhenAvailable`** with stubbed globals. The central assertion is the bug's own signature: a blocked visible tab makes exactly **one** `play()` call in a 300 ms window (the old code: thousands), a gesture earns exactly one retry, the cap ends a hopeless loop at 20 with the policy error surfaced, non-policy errors never retry, and the byte/alias cache accounting is exact. - Client lanes need DOM lib types, which conflict with the server lanes' node types, so they get their own tsconfig chained into **client** lint — resolving `@marinara-engine/shared` from *source*, because fresh-checkout CI lints before anything builds shared `dist/`. Verified by simulation: client lint passes with `packages/shared/dist` renamed away. - An adversarial multi-agent review ran before opening; all five confirmed findings are folded in (the parked-listener release, alias double-counting, the fresh-checkout CI break, toast dismissal, and the settings preview). Notably refuted by the review: per-chunk re-blocking on strict Safari, and the cap exhausting during normal backgrounded listening. - **Real-Safari verification still wanted**: I cannot run Safari from this environment. The reporter (or anyone on macOS/iOS with restrictive autoplay settings) should confirm: TTS on a blocked tab shows the toast instead of freezing, one tap resumes audio, and the stop button works while blocked. ## Docs and release impact - [x] No docs changes needed (CHANGELOG entry included) - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) ## UI evidence (if applicable) The visible change is the tap-to-play toast and the preview button's stop state while blocked — both need a real Safari session to demonstrate (see manual verification). <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Safari text-to-speech playback no longer repeatedly retries blocked audio or freezes the tab. * Blocked playback now waits for a tap, key press, or touch interaction before retrying. * Added a notification when playback is blocked. * TTS controls now reflect blocked playback and prevent conflicting actions. * Limited iPhone and iPad audio memory usage to improve stability, with improved cache eviction. * **Tests** * Added regression coverage for blocked playback, retry limits, user-gesture recovery, and audio cache limits. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-23 04:15:27 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#6981
No description provided.