Repository navigation
Announce engine closure on disposal and terminal reconnect failure - #1268
Open
hiroshihorie wants to merge 5 commits into
Open
hiroshihorie wants to merge 5 commits into
hiroshihorie wants to merge 5 commits into
Conversation
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the terminal engine-closure changes.
No open issues 🎉
What changed in this PR
This PR centralizes terminal engine closure so subscribers receive EngineClosingEvent before disposal or terminal reconnect cleanup begins. It complements the separate fixes for already-blocked buffer waits and disconnect-during-reconnect races.
Changes:
- Adds idempotent closure handling before Room and Engine disposal callbacks and after terminal reconnect failures.
- Rejects buffer waits started after closure while leaving recoverable reconnects open.
- Adds ten lifecycle regression tests and a patch changeset.
| File | Description |
|---|---|
test/core/engine_closing_test.dart |
Tests closure events, disposal ordering, reconnect outcomes, and post-closure waits. |
lib/src/core/room.dart |
Marks the engine closed before Room disposal callbacks run. |
lib/src/core/engine.dart |
Centralizes closure signaling and guards buffer waits after closure. |
.changes/engine-terminal-closure |
Records the lifecycle and buffer-wait fixes for release. |
Review effort: Balanced
hiroshihorie
marked this pull request as ready for review
November 2, 2026 16:53
hiroshihorie
requested review from
changt,
cloudwebrtc and
xianshijing-lk
as code owners
November 2, 2026 16:53
waitForBufferStatusLow() threw, or completed its wait with, the plain string 'Engine disconnected' once the engine closed. Data stream writes surface that error, so an app catching LiveKitException missed it. Both paths now use UnexpectedConnectionState, which client-sdk-js also throws when its engine is closed.
The room tears down its transports before it closes the engine, so a full reconnect that resumes during that teardown used to open a new signal socket. The test pauses the reconnect and the dispose against each other and fails on main without the disposal ordering in this PR.
hiroshihorie
force-pushed
the
hiroshi/close-engine-on-terminal-paths
branch
from
November 3, 2026 11:55
ec7a1a3 to
8d4c725
Compare
cloudwebrtc
approved these changes
Nov 4, 2026
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Disposing a Room or giving up on reconnection tore down the engine without marking it closed or emitting
EngineClosingEvent. Subscribers waiting for closure never received it, and an unrecoverable reconnect leftisClosedfalse.This change centralizes terminal closure in an idempotent internal
Engine.markClosed(). It marks the engine closed synchronously and queuesEngineClosingEventwhile the event emitter is still alive.Disposal sequence
Room.dispose()callsengine.markClosed()beforesuper.dispose()and before any disposal callbacks. This placement matters because Room's cleanup itself callsengine.cleanUp(), before it eventually callsengine.dispose(). Announcing closure only in Engine's constructor-registered disposal callback is too late for that first transport teardown.Engine.dispose()also callsmarkClosed()beforesuper.dispose(). Disposal callbacks run in reverse registration order, so callbacks registered later can safely await engine work that is already being cancelled. Engine's constructor-registered callback retains its cleanup sequence: clean up transports, dispose the emitter, dispose signal listeners, and release reliable receive state.Explicit disconnect, retry exhaustion, and unrecoverable reconnect failure use the same closure helper before cleanup. Recoverable retries and full-reconnect transport replacement do not close the engine. Buffer waits started after closure reject immediately, even if the retained channel reports a low buffer. Closing-event delivery remains asynchronous. Disposal does not await completion of arbitrary event handlers.
What it fixes for a user
A room disposed while a full reconnect was tearing down the old connection used to join again with the old token about a second later, as a new participant sid.
restartConnectionnow checks closure after the teardown and stops. A dispose or disconnect that lands later, while the reconnect is opening its new signal connection, still rejoins. That is pre-existing, the same onmain, and left for the session follow-up.Buffer wait error type
waitForBufferStatusLow()used to throw, or fail its wait with, the plain string'Engine disconnected'. Data stream writes surface that error, so an app catchingLiveKitExceptionmissed it. Both paths now useUnexpectedConnectionState, which client-sdk-js also throws when its engine is closed.Relationship to other fixes
mainand includes it.disconnect(), and now alsodispose(), raise an uncaughtUnexpectedConnectionStatewhile the write carries on.mainalready does this ondisconnect(), with the string error. With Fix waiting for the data channel send buffer to drain #1267 on top, the blocked write fails withUnexpectedConnectionStateand nothing is left uncaught.Validation
engine_closing_test.dartcover closure events, recoverable versus terminal reconnect failures, waits started after closure, duplicate closure, and disposal ordering. One verifies that the closing event is delivered with Engine marked disposed but its separate emitter still alive.disconnect_during_reconnect_test.dartdisposes the room while a full reconnect is still tearing down, the case where this PR meets Stay closed when disconnect() lands during a full reconnect #1264. It fails onmain, where the reconnect opens a new signal socket, and passes here.main'slib, 10 of the 13 tests in those two files fail. Removingengine.markClosed()fromRoom.dispose()fails 3 of them, removing the post-teardown_isClosedcheck inrestartConnectionfails 2.flutter analyze,dart analyze web/, repository format, import order, version consistency, andgit diff --checkpass.livekit-server --devpasses Connection basics, PingPong, duplicate identity, Delete Room, both data channel scenarios, full reconnect, disconnect during a full reconnect, migration and node failure.main(88c72c6), where the identity joins again with a new participant sid about 1 s afterdispose(), and passes in 3 of 3 here. On this head, the scenarios taggedreconnect,dataandsignalingpass, apart from expected failures and "Reliable data across a resume", the known gap until Send reliable data after a full reconnect instead of dropping it #1277.disconnect()ordispose()was checked with a throwaway test on both branches, with the results described above.