Skip to content

fix(leak): Memory leak when Room.connect() fails#652

Open
LautaroPetaccio wants to merge 1 commit intolivekit:mainfrom
LautaroPetaccio:fix/connect-error-listener-leak
Open

fix(leak): Memory leak when Room.connect() fails#652
LautaroPetaccio wants to merge 1 commit intolivekit:mainfrom
LautaroPetaccio:fix/connect-error-listener-leak

Conversation

@LautaroPetaccio
Copy link
Copy Markdown
Contributor

Summary

When Room.connect() fails, the onFfiEvent listener registered on the global FfiClient singleton is never removed. This causes two problems:

  • The Room instance cannot be garbage collected because FfiClient holds a reference to this.onFfiEvent, which closes over the Room.
  • preConnectEvents grows unboundedly because onFfiEvent keeps pushing every FfiEvent into the array — the guard !this.localParticipant || !this.ffiHandle || !this.info is always true for a Room that never finished connecting.
  • Retrying connect() on the same Room multiplies the leak, since each failed attempt adds another copy of the listener.

The fix wraps the connect logic in a try/catch so that on any error (whether from waitFor rejection or the ConnectError thrown on the 'error' case), we:

  1. Remove the onFfiEvent listener from FfiClient
  2. Clear the preConnectEvents array

The listener is intentionally not removed on the success path — it must stay attached for the room to function normally.

When connect() throws (either from a ConnectError or a rejected
waitFor), the onFfiEvent listener registered on the global FfiClient
singleton was never removed. This prevents the Room from being GC'd
and causes preConnectEvents to grow unboundedly because the handler
keeps pushing events into a Room that never finished connecting.

Wrap the connect logic in try/catch so the listener and stale
preConnectEvents are cleaned up on any error path.
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 14, 2026

🦋 Changeset detected

Latest commit: a132b5a

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

This PR includes changesets to release 1 package
Name Type
@livekit/rtc-node 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

@LautaroPetaccio LautaroPetaccio changed the title Fix memory leak when Room.connect() fails fix(leak): Memory leak when Room.connect() fails Apr 14, 2026
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: No Issues Found

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

View in Devin Review to see 3 additional findings.

Open in Devin Review

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.

1 participant