Skip to content

fix: close stream controllers on room disconnect#633

Open
LautaroPetaccio wants to merge 5 commits intolivekit:mainfrom
LautaroPetaccio:fix/close-stream-controllers-on-disconnect
Open

fix: close stream controllers on room disconnect#633
LautaroPetaccio wants to merge 5 commits intolivekit:mainfrom
LautaroPetaccio:fix/close-stream-controllers-on-disconnect

Conversation

@LautaroPetaccio
Copy link
Copy Markdown

Why

Room.disconnect() removed event listeners and called removeAllListeners(), but never closed the ReadableStreamDefaultController instances stored in byteStreamControllers and textStreamControllers. If a sender disconnects mid-transfer (so no trailer is ever received), the receiving side's stream stays open forever. Each open stream holds a controller reference and any buffered chunks, preventing GC.

How

After the FFI disconnect callback completes but before removing listeners, iterate both byteStreamControllers and textStreamControllers, call controller.close() on each (wrapped in try/catch since a controller may already be closed), and clear the maps. This ensures streams that never received a trailer are properly terminated.

Test coverage

New E2E test cleans up stream controllers when disconnecting during an active stream — opens a text stream, deliberately does not consume it, disconnects the receiver, and verifies no hang or throw from the abandoned controller.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 1, 2026

🦋 Changeset detected

Latest commit: cb2daa8

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

Copy link
Copy Markdown

@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 2 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