Skip to content

Fix data channel close race condition#1978

Open
1egoman wants to merge 5 commits into
mainfrom
fix-data-channel-close-race-condition
Open

Fix data channel close race condition#1978
1egoman wants to merge 5 commits into
mainfrom
fix-data-channel-close-race-condition

Conversation

@1egoman

@1egoman 1egoman commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Fixes #1953 (I think, still needs confirmation from the reporter - I was unable to reproduce locally).

In main, there is a possible race condition that could happen when disconnecting a room. The below three tasks run in this order:

  1. Close peer connection
  2. Close data channel
  3. Remove handlers from data channel

What could happen is that when 1 would run, it would close the data channels pre-emptively and lead to some of the handlers (3) firing before they were cleaned up, leading to a Unknown DataChannel error on ... error being logged.

So to fix this, reorder the above to now run in 3 -> 2 -> 1 order. This means that by the time any data channel errors that occur during disconnect are fired (at 1), the handlers are already detached (3).


As an unrelated fix, also, Unknown DataChannel error on ... shouldn't have been logged in this case. It looks like this was because of the event instanceof ErrorEvent check - actually, event would be a RTCErrorEvent and confusingly given their names, RTCErrorEvent does not extend ErrorEvent. So, alter this code to check the right error which should make these error paths show more detail.

1egoman added 4 commits June 16, 2026 11:32
…ving handlers

So now the order is:
1. Detach handlers
2. Run dc.close()
3. After 1+2 have run for all data channels, THEN call pcManager.close()
@changeset-bot

changeset-bot Bot commented Jun 16, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 8db768c

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 Jun 16, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size
dist/livekit-client.esm.mjs 100.64 KB (+0.16% 🔺)
dist/livekit-client.umd.js 109.54 KB (-0.01% 🔽)

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.

Unknown DataChannel error logged as console.error for graceful close events (audio-only rooms)

1 participant