Fix stream controller map leak on disconnect#651
Closed
LautaroPetaccio wants to merge 2 commits intolivekit:mainfrom
Closed
Fix stream controller map leak on disconnect#651LautaroPetaccio wants to merge 2 commits intolivekit:mainfrom
LautaroPetaccio wants to merge 2 commits intolivekit:mainfrom
Conversation
When a sender's stream is aborted, no trailer is sent, so the receiver never cleans up its byteStreamControllers/textStreamControllers Map entries. These entries leak for the lifetime of the Room object. Two complementary fixes: 1. Receiver side (room.ts): Add cleanupStreamControllers() called on disconnect() that errors any open controllers and clears both Maps. This ensures consumers get a proper error instead of hanging forever, and prevents stale entries from accumulating. 2. Sender side (participant.ts): In abort() handlers for streamText() and streamBytes(), attempt to send a DataStream_Trailer with the error reason. This is best-effort — if the transport is already down the send will fail silently, and the receiver-side cleanup handles that case.
🦋 Changeset detectedLatest commit: c23e2b8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
Contributor
Author
|
Closing: equivalent fix already merged upstream in #633 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a sender's data stream is aborted (e.g., due to a transport failure during
sendChunk), noDataStream_Traileris sent to the remote side. On the receiver,handleStreamTrailer()is the only code path that callscontroller.close()and removes entries from thebyteStreamControllers/textStreamControllersMaps. Sinceroom.disconnect()does not clear these Maps either, theStreamControllerobjects (containing header,ReadableStreamDefaultController, andstartTime) leak for the entire lifetime of the Room object.Changes
Receiver side (
room.ts):cleanupStreamControllers()that iterates both Maps, errors any open controllers (so consumers get a proper error instead of hanging indefinitely), and clears the entries.disconnect()before removing event listeners.Sender side (
participant.ts):abort()handlers in bothstreamText()andstreamBytes()to send aDataStream_Trailerwith the error reason on a best-effort basis. If the transport is already down, the send fails silently — the receiver-side cleanup handles that case.