[GH-ISSUE #5889] Safari TTS: infinite zero-delay play() retry loop freezes the tab (reported as "TTS freezes my instance") #3249

Closed
opened 2026-10-21 06:01:12 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @kolacheee on GitHub (Sep 6, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5889

Originally assigned to: @kolacheee on GitHub.

In one sentence: starting TTS in Safari can spin the client into an infinite zero-delay audio.play() retry loop that freezes the tab and balloons memory until WebKit kills it — reported in the field as "TTS freezes my whole Marinara instance," and reproducible in principle on any Safari with autoplay restrictions (iOS and macOS both).

Field report

Docker/Linux server, Safari on macOS + iOS. Starting xAI TTS (any voice, autoplay or manual) freezes the triggering client; Safari memory "shoots way up"; disabling TTS prevents it entirely; server console shows nothing unusual. Build 2.4.4+e390e36194a0 — but there are zero TTS changes between that commit and current staging, so this is live for everyone.

Mechanism (confirmed from source)

playWhenAvailable in packages/client/src/lib/tts-service.ts (~141-160) is while (true) { await audio.play() }. Its retry gate treats NotAllowedError as "wait until the document is visible and focused, then try again" — but Safari throws NotAllowedError for a policy reason (no live user activation), not a visibility reason. When the tab is already visible and focused — exactly the autoplay case — the wait resolves immediately and the loop retries play() with zero backoff, no cap, and no way to succeed, allocating a promise + DOMException per iteration on the main thread until the tab freezes.

Both entry points are affected:

  • Autoplay: fires seconds after generation ends, long past any transient user activation → NotAllowedError → spin.
  • Manual play: the click handler awaits the /api/tts/speak fetch before calling play(), which lands outside Safari's synchronous gesture window → same rejection on restrictive settings ("Never Auto-Play" / Stop Media with Sound) → same spin.

Two compounding findings:

  • The iOS in-memory audio cache (tts-audio-cache.ts) is capped at 150 entries with no byte cap (the persistent IndexedDB tier is deliberately disabled on iOS WebKit), so long sessions accumulate up to 150 live audio blobs — the slow memory balloon the reporter describes, additive to the fast one from the spin.
  • Enabling the roleplay speaker extractor makes TTS start fire an extra server-side LLM call — almost certainly the "one more prompt in the console right before the freeze" the reporter attributed to image generation.

What is proven NOT the cause

The server /speak path was audited at the reporter's commit and exonerated: it is a stateless binary proxy (zod-capped input, raw Buffer out; audio is never base64-encoded, never JSON-embedded, never written to the chat store; no shared locks; single-digit-millisecond synchronous cost for a 5 MB clip). The audio also never enters chat payloads, caches, or anything a second client would receive.

That honesty leaves one open remainder: the reporter says their desktop client seized at the same time and reloads gave a blank page — and neither confirmed mechanism explains that half (the desktop provably runs no TTS code for a mobile-triggered generation). Discriminator for the reporter, next episode:

curl -s -o /dev/null -w "%{time_total}\n" http://<server>:<port>/api/health

from any other machine during the freeze. Fast = server healthy, desktop symptom is its own tab (tell us what it was doing); slow = a server stall we have not found, and that timing is the lead.

Fix (PR incoming)

  • NotAllowedError in a visible, focused document becomes terminal for automatic retries: surface a blocked/tap-to-play state and re-arm playback from a real one-shot user-gesture listener instead of spinning.
  • Minimum retry delay + iteration cap in playWhenAvailable regardless of error type.
  • Manual play calls play() synchronously inside the click gesture on a pre-created element, then swaps in the fetched source, so Safari's gesture unlock applies.
  • Byte-based cap (with LRU eviction) on the in-memory audio cache tier.
Originally created by @kolacheee on GitHub (Sep 6, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5889 Originally assigned to: @kolacheee on GitHub. **In one sentence:** starting TTS in Safari can spin the client into an infinite zero-delay `audio.play()` retry loop that freezes the tab and balloons memory until WebKit kills it — reported in the field as "TTS freezes my whole Marinara instance," and reproducible in principle on any Safari with autoplay restrictions (iOS and macOS both). ## Field report Docker/Linux server, Safari on macOS + iOS. Starting xAI TTS (any voice, autoplay or manual) freezes the triggering client; Safari memory "shoots way up"; disabling TTS prevents it entirely; server console shows nothing unusual. Build `2.4.4+e390e36194a0` — but there are zero TTS changes between that commit and current staging, so this is live for everyone. ## Mechanism (confirmed from source) `playWhenAvailable` in `packages/client/src/lib/tts-service.ts` (~141-160) is `while (true) { await audio.play() }`. Its retry gate treats `NotAllowedError` as "wait until the document is visible and focused, then try again" — but Safari throws `NotAllowedError` for a *policy* reason (no live user activation), not a visibility reason. When the tab is already visible and focused — exactly the autoplay case — the wait resolves immediately and the loop retries `play()` with **zero backoff, no cap, and no way to succeed**, allocating a promise + DOMException per iteration on the main thread until the tab freezes. Both entry points are affected: - **Autoplay**: fires seconds after generation ends, long past any transient user activation → `NotAllowedError` → spin. - **Manual play**: the click handler awaits the `/api/tts/speak` fetch *before* calling `play()`, which lands outside Safari's synchronous gesture window → same rejection on restrictive settings ("Never Auto-Play" / Stop Media with Sound) → same spin. Two compounding findings: - The iOS in-memory audio cache (`tts-audio-cache.ts`) is capped at 150 *entries* with **no byte cap** (the persistent IndexedDB tier is deliberately disabled on iOS WebKit), so long sessions accumulate up to 150 live audio blobs — the slow memory balloon the reporter describes, additive to the fast one from the spin. - Enabling the roleplay speaker extractor makes TTS start fire an extra server-side LLM call — almost certainly the "one more prompt in the console right before the freeze" the reporter attributed to image generation. ## What is proven NOT the cause The server `/speak` path was audited at the reporter's commit and exonerated: it is a stateless binary proxy (zod-capped input, raw `Buffer` out; audio is never base64-encoded, never JSON-embedded, never written to the chat store; no shared locks; single-digit-millisecond synchronous cost for a 5 MB clip). The audio also never enters chat payloads, caches, or anything a second client would receive. That honesty leaves one **open remainder**: the reporter says their *desktop* client seized at the same time and reloads gave a blank page — and neither confirmed mechanism explains that half (the desktop provably runs no TTS code for a mobile-triggered generation). Discriminator for the reporter, next episode: ``` curl -s -o /dev/null -w "%{time_total}\n" http://<server>:<port>/api/health ``` from any other machine during the freeze. Fast = server healthy, desktop symptom is its own tab (tell us what it was doing); slow = a server stall we have not found, and that timing is the lead. ## Fix (PR incoming) - `NotAllowedError` in a visible, focused document becomes **terminal for automatic retries**: surface a blocked/tap-to-play state and re-arm playback from a real one-shot user-gesture listener instead of spinning. - Minimum retry delay + iteration cap in `playWhenAvailable` regardless of error type. - Manual play calls `play()` synchronously inside the click gesture on a pre-created element, then swaps in the fetched source, so Safari's gesture unlock applies. - Byte-based cap (with LRU eviction) on the in-memory audio cache tier.
bsakaguchi 2026-10-21 06:01:12 +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#3249
No description provided.