Skip to content

fix(voice): resolve video task leak in AgentSession#6318

Merged
longcw merged 1 commit into
livekit:mainfrom
Mayank-MSJ-Singh:video-leak-bug-fix
Jul 6, 2026
Merged

fix(voice): resolve video task leak in AgentSession#6318
longcw merged 1 commit into
livekit:mainfrom
Mayank-MSJ-Singh:video-leak-bug-fix

Conversation

@Mayank-MSJ-Singh

@Mayank-MSJ-Singh Mayank-MSJ-Singh commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

I was just checking this project and saw this, did some test and felt it might be a real bug, so i fixed it and created this PR!

Description

This change resolves a resource leak where the video forwarding task (_forward_video_atask) was left dangling in the background on session teardown.

The Problem

During session teardown (AgentSession._aclose_impl), the cleanup sequence explicitly cancels and awaits the audio forwarding task (_forward_audio_atask), but completely omitted _forward_video_atask.

This omission leads to leaks under two scenarios:

  1. Early Abort (No Activity): If a session starts and is aborted before an activity is initialized (so self._activity is None), the block detaching the inputs is skipped. The active video task is never cancelled and continues running indefinitely.
  2. Clean Teardown: Even when an activity is present, setting self.input.video = None cancels the active task but immediately schedules a new redundant task. Explicitly awaiting the task ensures the event loop is cleanly purged of any scheduled forwarding tasks.

The Fix

Added symmetric cancellation and cleanup in AgentSession._aclose_impl matching the audio task's handling:

if self._forward_video_atask is not None:
    await utils.aio.cancel_and_wait(self._forward_video_atask)

@Mayank-MSJ-Singh Mayank-MSJ-Singh requested a review from a team as a code owner July 4, 2026 18:31
@CLAassistant

CLAassistant commented Jul 4, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@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: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Open in Devin Review

@longcw longcw merged commit 19c55f9 into livekit:main Jul 6, 2026
17 checks passed
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.

3 participants