Room disconnects with `duplicateIdentity` after reconnecting from a single client: a timed-out signal connect leaves a live socket behind
Maintainer thường phản hồi trong vòng 2 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 30/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Lĩnh vực
- mobile-dev, networking
Hướng nghiên cứu
Read lib/src/core/signal_client.dart around SignalClient.connect() and the engine handler near engine.dart:~1603; trace what happens when the timed-out WebSocket connection completes late. The issue says the author already has a local fix and plans a PR, so check for that work before starting. Done means the late socket is closed, S2 remains connected, and no duplicateIdentity disconnect is emitted.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Describe the bug
While a room recovers from a network drop, it can end with RoomDisconnectedEvent(reason: DisconnectReason.duplicateIdentity), even though only one client uses that identity. The reconnect had already succeeded, and the connection was healthy when the event arrived.
duplicateIdentity is supposed to mean another participant joined with the same identity, so apps treat it as "you were replaced" and don't retry. Here, the other session is one this client opened during its own reconnect.
The server is behaving correctly. The client ends up with two sessions for one identity, the server kicks the older one, and the SDK applies that kick to the session it is actually using.
Root cause
SignalClient.connect() (lib/src/core/signal_client.dart, main @ 2.13.0):
var future = _wsConnector(rtcUri, options: WebSocketEventHandlers(
onData: _onSocketData, onDispose: _onSocketDispose, onError: _onSocketError), ...);
future = future.timeout(connectOptions.timeouts.connection);
_ws = await future;
Future.timeoutdoes not cancelio.WebSocket.connect. On a slow link the handshake can finish after the timeout. The server then has a live session S1 for that attempt.- That socket is never assigned to
_ws, socleanUp()can never close it.LiveKitWebSocketIOhas already subscribed it to the shared_onSocketData, so it keeps delivering messages as if it were current. - The engine retries, and the next join creates session S2, which is the one we keep. The server sees two sessions for one identity and sends
Leave(DISCONNECT, DUPLICATE_IDENTITY)to S1, which is the orphaned socket. - The leave comes through
_onSocketDataas an ordinarySignalLeaveEvent. The engine handler (engine.dart:~1603) runssignalClient.cleanUp()anddisconnect(), which tears down S2 and emitsRoomDisconnectedEvent(reason: duplicateIdentity). Signal is already disconnected at that point, so noleaveis sent for S2 either. The server keeps S2 as a stale participant until it times out, and other participants go on seeing it in the room.
To reproduce
Deterministic on an Android emulator (reproduced 5+ times):
- Use a short connect timeout so the race is easy to hit:
ConnectOptions(timeouts: Timeouts(connection: Duration(seconds: 3), ...)). - Turn on verbose logs:
hierarchicalLoggingEnabled = true; Logger('livekit').level = Level.ALL;. - Emulator: Wi-Fi off, so traffic goes over the emulated mobile-data link.
adb emu network delayonly affects that link. - Join a room.
- Airplane mode on for ~40 s, so the server drops the session.
adb emu network delay 5000. Every packet now takes longer than the connect timeout.- Airplane mode off.
About 30 s later the logs show:
Leave received, action: RECONNECT, reason: STATE_MISMATCH // resume rejected, server already dropped us
EngineFullRestartingEvent // full rejoin #1
Received JoinResponse // S1's handshake finished...
Connect Error TimeoutException after 0:00:03 // ...but the attempt had already timed out
EngineFullRestartingEvent // full rejoin #2 -> S2
Leave received, action: DISCONNECT, reason: DUPLICATE_IDENTITY // meant for S1, handled as S2's
RoomDisconnectedEvent(reason: duplicateIdentity)
Expected: the room stays connected on S2, S1's socket is closed, and no RoomDisconnectedEvent is emitted.
Actual: RoomDisconnectedEvent(reason: duplicateIdentity) on a single client.
We also see this in production on phones with poor mobile data: one device, one identity, and a duplicateIdentity disconnect right after a run of reconnect attempts.
Relation to #1211
#1211 adds a per-socket generation so callbacks from a replaced socket are ignored. Once the retry bumps the generation, that would also drop the late leave above. This issue is a different trigger (a timed-out attempt instead of a disposed one), and two gaps remain:
- the late socket is never closed, so it stays open and holds a server session until the server closes it;
- between the timeout and the next
connect()(e.g. during retry backoff), the orphan's callbacks still carry the current generation and are processed.
I have a fix with tests for this locally and plan to open a PR.
Environment
- livekit_client 2.7.0 in production, same code on main (2.13.0)
- Android (emulator repro) and iOS (production)
- LiveKit server (self-hosted)
- Ngôn ngữ chính
- Dart
- Star
- 421
- Fork
- 260
- Merge trung bình
- 2 ngày 5 giờ
- Pull request đã merge (30 ngày)
- 20
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của livekit/client-sdk-flutter
-
`jwtPayload.roomConfiguration` returns null fields for tokens from LiveKit server SDKsCó thể đã có người làm @RaphaelFakhri đã nhận 15 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
livekit/client-sdk-flutter#1243 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
[bug] `AudioPublishOptions.red` is inverted: the default `red: true` sends `disableRed: true`, disabling RED for every audio publishCó thể đã có người làm @hiroshihorie đã nhận 2 ngày trước. Đang mở
livekit/client-sdk-flutter#1271 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 38/100
livekit/client-sdk-flutter#1265 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
A full reconnect that is in progress when disconnect() is called reopens the closed engine and retries up to reconnectAttemptsCó thể đã có người làm @hiroshihorie đã nhận 6 ngày trước. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 57/100
livekit/client-sdk-flutter#1245 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
livekit/client-sdk-flutter#1227 ·
Maintainer thường phản hồi trong vòng 2 ngày
Tất cả issue của livekit/client-sdk-flutter
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
libnativeapi/nativeapi#21 ·
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
chriskelly/wish-together#102 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area/adaptivity kind/task priority/p1
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 60/100
spotiflacapp/SpotiFLAC-Mobile#783 ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày