Skip to content

fix: avoid attaching a new Closing event listener for each waitForBufferStatusLow call#1896

Merged
1egoman merged 4 commits intomainfrom
fix-closing-max-listeners-warning
Apr 20, 2026
Merged

fix: avoid attaching a new Closing event listener for each waitForBufferStatusLow call#1896
1egoman merged 4 commits intomainfrom
fix-closing-max-listeners-warning

Conversation

@1egoman
Copy link
Copy Markdown
Contributor

@1egoman 1egoman commented Apr 17, 2026

In the waitForBufferStatusLow method of RTCEngine, a bunch of this.once calls are currently being made to listen for RTCEngine Closing events. This can result potentially in errors like the below if waitForBufferStatusLow is called at a high rate (the below was captured in firefox, fwiw):

MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 closing  
listeners added. Use emitter.setMaxListeners() to increase limit                     
    _addListener events.js:211                                                       
    addListener events.js:227                                                        
    once events.js:257                                                               
    waitForBufferStatusLow RTCEngine.ts:1569                                         
    __awaiter livekit-client.esm.mjs:8244                                            
    __awaiter livekit-client.esm.mjs:8240                                            
    waitForBufferStatusLow RTCEngine.ts:1561                                         
    TypedPromise TypedPromise.ts:8                                                   
    waitForBufferStatusLow RTCEngine.ts:1561                                         
    __awaiter livekit-client.esm.mjs:8244                                            
    __awaiter livekit-client.esm.mjs:8240                                            
    waitForBufferStatusLow livekit-client.esm.mjs:21539                              
    sendLossyBytes RTCEngine.ts:1499                                                 
    fulfilled livekit-client.esm.mjs:8241                                            
    promise callback*step livekit-client.esm.mjs:8243                                
    __awaiter livekit-client.esm.mjs:8244                                            
    __awaiter livekit-client.esm.mjs:8240                                            
    sendLossyBytes RTCEngine.ts:1485                                                 
    Room Room.ts:294                                                                 
    emit events.js:153                                                               
    tryProcessAndSend OutgoingDataTrackManager.ts:157                                

While this seems to have been a problem in theory for a while, the recent shift to listening to bufferedamountlow events in #1877 rather than await sleep(10) in a loop I think has made this inadvertently worse, as the sleeps previously were giving the listeners time to drain and keeping the RtcEngine listener count below the threshold.

So instead, listen once and fan out that single event to all in flight waitForBufferStatusLow calls.

…ferStatusLow call

This can very quickly start to log lots of warnings.
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 17, 2026

🦋 Changeset detected

Latest commit: 1e97f7c

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

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

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 17, 2026

size-limit report 📦

Path Size
dist/livekit-client.esm.mjs 96.23 KB (+0.1% 🔺)
dist/livekit-client.umd.js 105.14 KB (+0.02% 🔺)

@1egoman
Copy link
Copy Markdown
Contributor Author

1egoman commented Apr 17, 2026

I ran the end to end tests, and got a few failures which looked to be just flaky tests. So I don't think this broke anything.

Comment thread src/room/RTCEngine.ts Outdated
@1egoman 1egoman merged commit 6696692 into main Apr 20, 2026
6 checks passed
@1egoman 1egoman deleted the fix-closing-max-listeners-warning branch April 20, 2026 16:54
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.

2 participants