Skip to content

fix(livekit): Memory leak during cancel publisher negotiation when rooms close - #1538

Merged
alan-george-lk merged 2 commits into
mainfrom
alan/bugfix-session-inner-leak
Nov 18, 2026
Merged

alan-george-lk merged 2 commits into
mainfrom
alan/bugfix-session-inner-leak

Conversation

@alan-george-lk

@alan-george-lk alan-george-lk commented Oct 19, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

This leak was detected during C++ regression test work catching memory leaks, see here: https://github.com/livekit/client-sdk-cpp/actions/runs/43513539718#summary-132669981633

It happens intermittently, but in troubleshooting with GPT this issue was identified.

Changes:

  • Cancel in-flight fast-publish and debounced publisher negotiations through the session close signal
  • Ignore publisher negotiation requests after session closure
  • Add an ownership regression test and release changeset

Root cause

Publisher negotiation ran in detached Tokio tasks that strongly retained SessionInner. During rapid video publish, unpublish, and disconnect cycles, a task could remain waiting up to 10 seconds for an SDP answer and then retry after the room had closed. The retained session kept its peer connections, peer connection factory, media threads, and encoder resources alive.

Validation

  • New focused ownership cancellation test passed
  • In the C++ SDK, reproduced before the fix under taskset -c 0,1: run 6 gained 8 threads and 64.44 MiB
  • After the fix, 12 consecutive 100-cycle constrained-CPU video runs passed with thread count stable at 7

Fixes BOT-586

@github-actions

Copy link
Copy Markdown
Contributor

Changeset ✓

This PR includes a changeset covering all affected packages:

Package Bump
livekit patch
livekit-capture patch
livekit-ffi patch

@alan-george-lk
alan-george-lk force-pushed the alan/bugfix-session-inner-leak branch from 933fed4 to 51fef32 Compare November 9, 2026 19:29
@alan-george-lk alan-george-lk changed the title Cancel publisher negotiation when rooms close fix(livekit): Memory leak, cancel publisher negotiation when rooms close Nov 16, 2026
@alan-george-lk alan-george-lk changed the title fix(livekit): Memory leak, cancel publisher negotiation when rooms close fix(livekit): Memory leak during cancel publisher negotiation when rooms close Nov 16, 2026
@alan-george-lk
alan-george-lk marked this pull request as ready for review November 16, 2026 20:49

@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: 1 flag

Not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

@alan-george-lk
alan-george-lk force-pushed the alan/bugfix-session-inner-leak branch from 0a0fbe3 to 793eb1f Compare November 16, 2026 20:55

@ladvoc ladvoc 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.

LGTM ✅. The complexity of this fix indicates we're due for a larger refactor, but given the current state of things, I'm not sure you can do much better.

@alan-george-lk
alan-george-lk merged commit 96fae7b into main Nov 18, 2026
98 of 104 checks passed
@alan-george-lk
alan-george-lk deleted the alan/bugfix-session-inner-leak branch November 18, 2026 19:36
@alan-george-lk

Copy link
Copy Markdown
Collaborator Author

Note: bypassed CI failure as confirmed it was unrelated to this change and was holding up a release.

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