Skip to content

CloudTtsEngine: stop()/speak() don't cancel in-flight chunk 2+ synthesis, letting stale audio resurrect after stop #469

Open

Description

@github-actions

Where

core/cloudtts/src/main/kotlin/xyz/libravault/core/cloudtts/CloudTtsEngine.kt, introduced in #468 (speak/speakChunk/stop).

What's broken

speak(text) only tracks the coroutine for the first chunk:

speakJob = scope.launch {
    val chunks = splitIntoChunks(text)
    speakChunk(chunks, 0)
}

speakChunk is non-suspending once it reaches playback — it calls cloudTtsProvider.synthesize() (suspend), then playback.play(audioBytes, onCompletion = { scope.launch { speakChunk(chunks, index + 1) } }, ...). playback.play itself doesn't suspend; it kicks off MediaPlayer and returns immediately. So the coroutine assigned to speakJob completes as soon as the first chunk starts playing — well before the utterance is actually done.

Every subsequent chunk (index >= 1) is driven by a new, untracked scope.launch { speakChunk(chunks, index + 1) } fired from the onCompletion/onError callback of the previous chunk's playback. Nothing stores a reference to these jobs. stop() and speak() both only do speakJob?.cancel() (and fallbackForwardingJob?.cancel()), which cannot reach these later, untracked coroutines — they are siblings under the shared singleton scope (CoroutineScope(Dispatchers.Default) from TtsModule), not children of speakJob.

scope is a @Singleton bound for the app's whole process lifetime (TtsModule.provideTtsCoroutineScope), and CloudTtsEngine/MediaPlayerCloudPlayback are themselves @Singleton — so a leaked chunk-continuation coroutine isn't cleaned up by anything short of process death.

Concrete failure scenario

  1. User is on a long chapter (> MAX_CHUNK_CHARS = 3900 chars, so speak() splits it into 2+ chunks) with Cloud TTS active.
  2. Chunk 0 finishes playing. onCompletion fires scope.launch { speakChunk(chunks, 1) } (call it Job B) — untracked.
  3. Job B suspends inside cloudTtsProvider.synthesize() awaiting the vendor's HTTP response for chunk 1.
  4. User presses Stop (or TtsEngineProvider.switchEngine swaps to a different TtsEngineType, which also calls oldEngine.stop()).
  5. stop() runs: speakJob?.cancel() is a no-op (that job already finished in step 2), playback.stop() stops the already-finished chunk-0 player, state is set to IDLE, _stopEvent fires. Job B keeps running.
  6. Chunk 1's HTTP response arrives later; Job B resumes and calls playback.play(audioBytes, ...), which starts a new MediaPlayer and begins audio output — after the engine reported itself stopped/idle, possibly after the user switched to a different TTS engine or left the screen entirely.

Net effect: audio can resume playing after the user stopped it (or switched engines), and a paid, user-supplied-API-key HTTP call that should have been aborted on stop() fires anyway.

Why it wasn't caught

CloudTtsEngineTest only exercises texts like "hello"/"hello world" — always under MAX_CHUNK_CHARS, so every test stays on speakChunk(chunks, 0), the one path that is tracked by speakJob. There's no test with multi-chunk input, so the untracked-continuation path is never exercised. AGENTS.md calls for boundary-case coverage for new code paths; the chunk-boundary path is exactly the gap.

Suggested fix direction

Track the whole chunk chain as a single cancellable unit — e.g. make speakChunk recurse via a suspending call awaited from inside the original speakJob coroutine (using a CompletableDeferred/suspendCancellableCoroutine to bridge playback.play's callback back into suspend land) instead of firing a fresh top-level scope.launch from onCompletion. That way speakJob?.cancel() in stop()/speak() cancels the entire remaining chain, not just whichever chunk happens to be first.

Filed per principal-review-agent policy: this is a CONFIRMED-severity finding from PR #468's review, tracked here regardless of whether it's fixed inline in that PR or as a follow-up.

Activity

  1. added
    bug
    on Aug 23, 2026
  2. libravault-xyz commented on Aug 23, 2026

    @libravault-xyz
    Collaborator

    Automated pipeline health check: Flagging an anomaly — doesn't match this routine's 5 known failure patterns, and no comment has been posted on this issue yet.

    This issue's author is github-actions[bot] (CONTRIBUTOR association) — the same profile that trips dev-agent.yml's author-association trust gate on #388/#408/#409/#433/#437/#441 (see #453, fixed via #454, merged 2026-08-23T08:19:21). But unlike that batch, which each got a skipped conclusion run within seconds of filing, this issue has received zero dev-agent.yml runs at all as of this check (filed 2026-08-23T14:14:34; gh run list --workflow=dev-agent.yml -e issues shows nothing newer than 12:51:42). Since #454 already merged, the expectation post-fix is a skipped run plus an explanatory comment — neither happened here.

    Possible causes, unconfirmed: (a) a one-off dropped webhook delivery for this specific issues: opened event (GitHub doesn't guarantee delivery — same class of gap #455 already documents for labeled events), or (b) something about #454's fix not covering this exact trigger path. Not enough evidence to pick one, and not self-healing either way — recommend a human check whether the issues: opened webhook actually reached Actions for this issue, and apply status:triage/status:ready-for-dev manually via workflow_dispatch if not.


    Generated by Claude Code

  3. libravault-xyz commented on Aug 23, 2026

    @libravault-xyz
    Collaborator

    Automated pipeline health check — update: The anomaly flagged above (zero dev-agent.yml runs for this issue) is no longer isolated. Two more github-actions[bot]-authored issues filed since have the same signature: #475 (filed 17:20:53Z) and #479 (filed 18:34:41Z) — both still carry no status label and zero comments, same as this one.

    Confirmed via the full issues-event run history for dev-agent.yml: the last such run was at 2026-08-23T12:51:42Z ("Verify AwsSigV4Signer..."). Nothing has fired since, across three separate issues:opened events spanning 14:14Z–18:34Z. Before that cutoff, issues-triggered runs were firing normally and frequently (6 in the preceding ~4.5h, all github-actions[bot]-authored, all correctly processed).

    Ruled out: no commit has touched .github/workflows/dev-agent.yml since #454 merged at 08:19:19Z, well before the 12:51:42Z cutoff — so this isn't a regression from that fix's own code. That leaves a GitHub-side webhook delivery gap for issues:opened on this repo as the likelier cause, but three consecutive misses over 6+ hours (after correctly firing 6/6 times earlier the same day) is past what I'd chalk up to ordinary one-off delivery loss.

    Still not self-healing: none of the three affected issues carry a status label to toggle, and I'm not authorized to invent a workflow_dispatch triage/scope decision for a new pattern. Recommend a human check whether GitHub Actions is actually receiving issues:opened webhook deliveries for this repo (repo Settings → Webhooks → Recent Deliveries, or GitHub's status page), and manually workflow_dispatch triage for #469/#475/#479 if not. Worth filing as its own tracked issue if confirmed — doesn't match any of the 5 known patterns this routine watches for.


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions