-
Notifications
You must be signed in to change notification settings - Fork 110
Fix stream controller map leak on disconnect #651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
LautaroPetaccio
wants to merge
2
commits into
livekit:main
from
LautaroPetaccio:fix/stream-controller-map-leak
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@livekit/rtc-node': patch | ||
| --- | ||
|
|
||
| Fix stream controller map leak on disconnect and abort |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| // SPDX-FileCopyrightText: 2024 LiveKit, Inc. | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| import { DataStream_Chunk, DataStream_Header } from '@livekit/rtc-ffi-bindings'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { Room } from '../room.js'; | ||
|
|
||
| function createMockStreamController() { | ||
| let controller!: ReadableStreamDefaultController<DataStream_Chunk>; | ||
| const stream = new ReadableStream<DataStream_Chunk>({ | ||
| start(c) { | ||
| controller = c; | ||
| }, | ||
| }); | ||
|
|
||
| // Keep a reader so the stream stays alive | ||
| const reader = stream.getReader(); | ||
|
|
||
| return { | ||
| header: new DataStream_Header(), | ||
| controller, | ||
| startTime: Date.now(), | ||
| reader, | ||
| }; | ||
| } | ||
|
|
||
| describe('Room', () => { | ||
| describe('cleanupStreamControllers', () => { | ||
| it('should clear byteStreamControllers on disconnect cleanup', () => { | ||
| const room = new Room(); | ||
| const roomAny = room as any; | ||
|
|
||
| const mock = createMockStreamController(); | ||
| roomAny.byteStreamControllers.set('stream-1', { | ||
| header: mock.header, | ||
| controller: mock.controller, | ||
| startTime: mock.startTime, | ||
| }); | ||
|
|
||
| expect(roomAny.byteStreamControllers.size).toBe(1); | ||
|
|
||
| roomAny.cleanupStreamControllers(); | ||
|
|
||
| expect(roomAny.byteStreamControllers.size).toBe(0); | ||
| }); | ||
|
|
||
| it('should clear textStreamControllers on disconnect cleanup', () => { | ||
| const room = new Room(); | ||
| const roomAny = room as any; | ||
|
|
||
| const mock = createMockStreamController(); | ||
| roomAny.textStreamControllers.set('stream-1', { | ||
| header: mock.header, | ||
| controller: mock.controller, | ||
| startTime: mock.startTime, | ||
| }); | ||
|
|
||
| expect(roomAny.textStreamControllers.size).toBe(1); | ||
|
|
||
| roomAny.cleanupStreamControllers(); | ||
|
|
||
| expect(roomAny.textStreamControllers.size).toBe(0); | ||
| }); | ||
|
|
||
| it('should error open stream controllers so consumers are not left hanging', async () => { | ||
| const room = new Room(); | ||
| const roomAny = room as any; | ||
|
|
||
| const mock = createMockStreamController(); | ||
| roomAny.byteStreamControllers.set('stream-1', { | ||
| header: mock.header, | ||
| controller: mock.controller, | ||
| startTime: mock.startTime, | ||
| }); | ||
|
|
||
| roomAny.cleanupStreamControllers(); | ||
|
|
||
| await expect(mock.reader.read()).rejects.toThrow('Room disconnected'); | ||
| }); | ||
|
|
||
| it('should handle cleanup when controllers are already closed', () => { | ||
| const room = new Room(); | ||
| const roomAny = room as any; | ||
|
|
||
| const mock = createMockStreamController(); | ||
| // Close the controller before cleanup | ||
| mock.controller.close(); | ||
|
|
||
| roomAny.byteStreamControllers.set('stream-1', { | ||
| header: mock.header, | ||
| controller: mock.controller, | ||
| startTime: mock.startTime, | ||
| }); | ||
|
|
||
| // Should not throw | ||
| roomAny.cleanupStreamControllers(); | ||
|
|
||
| expect(roomAny.byteStreamControllers.size).toBe(0); | ||
| }); | ||
|
|
||
| it('should clean up multiple streams from both maps', () => { | ||
| const room = new Room(); | ||
| const roomAny = room as any; | ||
|
|
||
| const byteMock1 = createMockStreamController(); | ||
| const byteMock2 = createMockStreamController(); | ||
| const textMock1 = createMockStreamController(); | ||
|
|
||
| roomAny.byteStreamControllers.set('byte-1', { | ||
| header: byteMock1.header, | ||
| controller: byteMock1.controller, | ||
| startTime: byteMock1.startTime, | ||
| }); | ||
| roomAny.byteStreamControllers.set('byte-2', { | ||
| header: byteMock2.header, | ||
| controller: byteMock2.controller, | ||
| startTime: byteMock2.startTime, | ||
| }); | ||
| roomAny.textStreamControllers.set('text-1', { | ||
| header: textMock1.header, | ||
| controller: textMock1.controller, | ||
| startTime: textMock1.startTime, | ||
| }); | ||
|
|
||
| expect(roomAny.byteStreamControllers.size).toBe(2); | ||
| expect(roomAny.textStreamControllers.size).toBe(1); | ||
|
|
||
| roomAny.cleanupStreamControllers(); | ||
|
|
||
| expect(roomAny.byteStreamControllers.size).toBe(0); | ||
| expect(roomAny.textStreamControllers.size).toBe(0); | ||
| }); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.