Skip to content

Fix RTCEngine.negotiate EngineEvent.Closing / EngineEvent.Restarting listeners to not subscribe to a new listener every call - #2084

Open
1egoman wants to merge 3 commits into
mainfrom
fix-negotiate-listener-accumulation
Open

Fix RTCEngine.negotiate EngineEvent.Closing / EngineEvent.Restarting listeners to not subscribe to a new listener every call#2084
1egoman wants to merge 3 commits into
mainfrom
fix-negotiate-listener-accumulation

Conversation

@1egoman

@1egoman 1egoman commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Previously, every negotiate call would add a new listener for EngineEvent.Closing and EngineEvent.Restarting. If this path was hit at too frequent of a rate, many listeners would get added and a MaxListenersExceededWarning would get logged.

Instead, aim for an approach similar to #1896 - attach the listener in one place, and fan out this one listener entry to all negotiate calls dynamically.

Fixes the first part (point 1) of #1915. The other two points need some further discussion / thought but this one makes a lot of sense to me and was fairly unambiguous.

…listener for each negotiation

This avoids the MaxListenersExceededWarning which could happen
previously if a bunch of listeners were all added at once.
@changeset-bot

changeset-bot Bot commented Sep 3, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 02163c0

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

This PR includes changesets to release 1 package
Name Type
livekit-client 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

@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size
dist/livekit-client.esm.mjs 110.95 KB (+0.06% 🔺)
dist/livekit-client.umd.js 120.09 KB (+0.03% 🔺)

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

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 bugs or issues to report.

Devin Review

Comment thread src/room/RTCEngine.ts
Comment on lines +312 to +319
const abortPendingNegotiations = () => {
// Iterate a copy: each handler removes itself from the set as its negotiation settles.
for (const abort of Array.from(this.pendingNegotiationAborts)) {
abort();
}
};
this.on(EngineEvent.Closing, abortPendingNegotiations);
this.on(EngineEvent.Restarting, abortPendingNegotiations);

@1egoman 1egoman Sep 3, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to reviewers - an important deviation from the example pull request linked in the description is I opted not to use a Future because the pending list needs to be resettable. There also isn't a super elegant way to remove a listener in the finally block in the same way with this solution - I am calling this.pendingNegotiationAborts.delete to do this.

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