Skip to content

Don't retain recorded events when recording is disabled#1750

Merged
toubatbrian merged 2 commits into
1.5.0from
brian/recorded-events-memory-leak
Jun 10, 2026
Merged

Don't retain recorded events when recording is disabled#1750
toubatbrian merged 2 commits into
1.5.0from
brian/recorded-events-memory-leak

Conversation

@toubatbrian

@toubatbrian toubatbrian commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Problem

AgentSession.emit() pushed every emitted event into _recordedEvents unconditionally:

const eventData = args[0] as AgentEvent;
this._recordedEvents.push(eventData);

emit fires for every agent event for the whole session (state changes, transcripts, conversation_item_added, usage, etc.), and each AgentEvent pins a large object graph (SpeechHandle, OTel spans/contexts, streams, chat items). The buffer is only cleared at _onSessionEnd, so over a long-running session it grows unbounded and leaks memory — even when recording is disabled (the common default).

Fix

Only push when _enableRecording is true. _recordedEvents is consumed solely by makeSessionReport(), and that report is only uploaded when recording is enabled, so this changes no observable behavior:

  • recording on: unchanged.
  • recording off: the buffer stays empty (it was never used anyway), eliminating the leak.

Notes

Targeted at 1.5.0. Minimal, behavior-preserving change; no changeset added since this is a release-branch patch (happy to add one if required).

AgentSession.emit() pushed every event into _recordedEvents for the whole
session, even when recording was off. Each AgentEvent pins a large object
graph (SpeechHandle, OTel spans/contexts, streams), so the buffer grew
unbounded and leaked memory over long sessions. _recordedEvents is only
consumed by makeSessionReport() (uploaded only when recording is enabled),
so gate the push on _enableRecording.

Co-authored-by: Cursor <cursoragent@cursor.com>
@changeset-bot

changeset-bot Bot commented Jun 10, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3265551

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

This PR includes changesets to release 34 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-fishaudio Patch
@livekit/agents-plugin-google Patch
@livekit/agents-plugin-hedra Patch
@livekit/agents-plugin-hume 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-mistralai Patch
@livekit/agents-plugin-neuphonic Patch
@livekit/agents-plugin-openai Patch
@livekit/agents-plugin-perplexity 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-plugin-soniox Patch
@livekit/agents-plugin-tavus 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

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

Copy link
Copy Markdown
Contributor

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 1 additional finding in Devin Review.

Open in Devin Review

Comment thread agents/src/voice/agent_session.ts
@toubatbrian toubatbrian merged commit 9d86bc5 into 1.5.0 Jun 10, 2026
2 checks passed
@toubatbrian toubatbrian deleted the brian/recorded-events-memory-leak branch June 10, 2026 23:11
tinalenguyen added a commit that referenced this pull request Jun 23, 2026
Squashed local branch work, reconciled with the latest remote state:

- Refactor ToolContext to parity class taking a list of Tool | Toolset (#1517)
- feat(agents): add Toolset support to ToolContext and AgentActivity (#1525)
- feat(agents): add beta end call tool (#1474)
- fix(agents): thread FlushSentinel through Agent.create llmNode types
- fix(elevenlabs): end server vad turns (#1745)
- Add Inworld delivery mode inference TTS option (#1749)
- Don't retain recorded events when recording is disabled (#1750)
- fix(deepgram): validate updated model with stored language on updateOptions
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