Skip to content

fix: emit room events that arrive right after a reconnect - #2186

Merged
1egoman merged 3 commits into
livekit:mainfrom
ANDROIDHASSAN:fix/flush-events-after-reconnect
Nov 16, 2026
Merged

1egoman merged 3 commits into
livekit:mainfrom
ANDROIDHASSAN:fix/flush-events-after-reconnect

Conversation

@ANDROIDHASSAN

@ANDROIDHASSAN ANDROIDHASSAN commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2183

After a reconnect, the room sets itself Connected, emits Reconnected and flushes its event buffer. But the engine only cleared reconnectTimeout in the .finally() of the scheduled attempt, which runs after that. Until then engine.pendingReconnect is still true, so emitWhenConnected() keeps buffering. Nothing flushes the buffer again until the next reconnect, and handleSignalRestarted() clears it at that point, so those events are lost.

reconnectTimeout is now cleared when the timer fires (and in clearReconnectTimeout()), and the .finally() is gone, so pendingReconnect is just !!this.reconnectTimeout and drops as soon as the attempt starts.

Two related cases (thanks @1egoman) are covered by the same change: handleBrowserOnLine cancelling the timer, and a failed attempt's retry handle being cleared by the old attempt's .finally(). main has since fixed both on its own (the handle guard in the .finally() and the clear in clearReconnectTimeout()); after merging main, this PR keeps the clear in clearReconnectTimeout() and replaces the guarded .finally() with the clear in the timer callback.

Tests in RTCEngine.test.ts (pendingReconnect (#2183)):

  • is cleared once the scheduled attempt starts — fails on current main, passes here
  • is cleared when the scheduled attempt is cancelled — passes on both (regression guard)
  • keeps a retry scheduled while the previous attempt settles — passes on both (regression guard)

main's own keeps pendingReconnect set while a retry is scheduled after a failed attempt still passes. Full vitest run src (61 files, 1075 passed, 1 skipped), pnpm lint (0 errors), tsc --noEmit and prettier --check pass after the merge.

pendingReconnect stayed true until the reconnect attempt's promise settled, after the room was already Connected and had flushed its buffer, so events emitted in that window were buffered and never emitted. It now only covers a reconnect that is scheduled but not started.
@changeset-bot

changeset-bot Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2bc830c

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-25T15:26:37.568464Z
We couldn’t complete this review. Comment @codex review to try again.
2cf921a PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@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 Resolved
Clear the field inside the timer callback and in clearReconnectTimeout
instead of in the attempt's finally, so a cancelled timer no longer
leaves pendingReconnect stuck and a finished attempt no longer clobbers
the retry scheduled during it. Moves the tests to RTCEngine.
@1egoman

1egoman commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

@ANDROIDHASSAN I think this makes sense to me, would you be able to rebase this on top of the latest origin/main? It looks like there are some conflicts. After you rebase this, I think this should be good to merge!

…ter-reconnect

# Conflicts:
#	src/room/RTCEngine.test.ts
#	src/room/RTCEngine.ts
@ANDROIDHASSAN

Copy link
Copy Markdown
Contributor Author

Merged main in instead of rebasing so the review history stays. The conflict was in RTCEngine.ts, where main had added a handle guard to the .finally(). I kept the clear in the timer callback instead, which covers that case too. Full suite, lint and tsc pass, and the body is updated.

@1egoman
1egoman merged commit 4c52f3d into livekit:main Nov 16, 2026
6 checks passed
@github-actions github-actions Bot mentioned this pull request Nov 16, 2026
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.

Events emitted while engine.pendingReconnect is still true after Reconnected stay in bufferedEvents until the next reconnect

2 participants