Skip to content

fix(voice): prevent RecorderIO shutdown write-loop noise#1378

Merged
toubatbrian merged 2 commits intomainfrom
brian/fix-recorder-stream-close
May 3, 2026
Merged

fix(voice): prevent RecorderIO shutdown write-loop noise#1378
toubatbrian merged 2 commits intomainfrom
brian/fix-recorder-stream-close

Conversation

@toubatbrian
Copy link
Copy Markdown
Contributor

Summary

  • Hardened RecorderIO shutdown to prevent repeated writes during teardown:
    • Set a shutdown fence (closing) and stop writes immediately when close starts
    • Cancel forwardTask before closing channels
    • Exit forward loop on closed-stream/channel conditions
  • Centralized isWritableStreamClosedError() into agents/src/utils.ts for reuse
  • Added focused regression tests for writable-stream closed error detection in:
    • agents/src/voice/recorder_io/recorder_io.test.ts

Why

On disconnect, recorder background writes could race with stream closure and emit repeated
Invalid state: WritableStream is closed errors. This change makes post-close writes structurally
impossible and treats teardown-expected closure as terminal, removing log spam.

Test Plan

  • pnpm test agents/src/voice/recorder_io/recorder_io.test.ts
  • Manual verification with multiple back-to-back session disconnects:
    • Confirm no repeated Error writing RecorderIO output buffer logs after disconnect

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 2, 2026

🦋 Changeset detected

Latest commit: ff7b80f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 28 packages
Name Type
@livekit/agents Patch
@livekit/agents-plugin-anam Patch
@livekit/agents-plugin-assemblyai Patch
@livekit/agents-plugin-baseten Patch
@livekit/agents-plugin-bey Patch
@livekit/agents-plugin-cartesia Patch
@livekit/agents-plugin-cerebras Patch
@livekit/agents-plugin-deepgram Patch
@livekit/agents-plugin-elevenlabs Patch
@livekit/agents-plugin-google Patch
@livekit/agents-plugin-hedra Patch
@livekit/agents-plugin-inworld Patch
@livekit/agents-plugin-lemonslice Patch
@livekit/agents-plugin-liveavatar Patch
@livekit/agents-plugin-livekit Patch
@livekit/agents-plugin-minimax Patch
@livekit/agents-plugin-mistral Patch
@livekit/agents-plugin-neuphonic Patch
@livekit/agents-plugin-openai Patch
@livekit/agents-plugin-phonic Patch
@livekit/agents-plugin-resemble Patch
@livekit/agents-plugin-rime Patch
@livekit/agents-plugin-runway Patch
@livekit/agents-plugin-sarvam Patch
@livekit/agents-plugin-silero Patch
@livekit/agents-plugins-test Patch
@livekit/agents-plugin-trugen Patch
@livekit/agents-plugin-xai Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ca3e31a95e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread agents/src/utils.ts
Comment on lines +773 to +774
if ('code' in error && (error as { code?: string }).code === 'ERR_INVALID_STATE') {
return true;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restrict closed-stream detection to closure-specific cases

isWritableStreamClosedError() currently returns true for any error with code === 'ERR_INVALID_STATE', but that code is also used for non-closure stream-state failures; when this happens, RecorderIO.forward() will treat a real stream bug as expected teardown and break the loop without surfacing it. This can silently stop recording progress and hide actionable errors, so the predicate should also verify the message (or otherwise positively identify closed-stream conditions) before suppressing/log-skipping.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

View 4 additional findings in Devin Review.

Open in Devin Review

Comment thread agents/src/voice/recorder_io/recorder_io.ts
@toubatbrian toubatbrian merged commit 5898229 into main May 3, 2026
9 checks passed
@toubatbrian toubatbrian deleted the brian/fix-recorder-stream-close branch May 3, 2026 08:05
@github-actions github-actions Bot mentioned this pull request May 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants