Repository navigation
fix(rtc): avoid Room.sid hang when disconnected or cancelled (#651) #875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
86afe63
2bf92fb
2762cbe
f1e0e00
d567e4a
500b900
ced3c84
296530f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Duplicate departures produce false error logs When a departed participant appears in another disconnect event, (Refers to this code) Learn moreThe room tracks remote participants by identity and removes an entry on a disconnect event. A duplicate or late disconnect event has no corresponding entry. The bare pop raises Example: Alice disconnects and the first event removes Alice from the map. A second disconnect event for Alice raises Recommended fix: Use Was this helpful? React with 👍 or 👎 to provide feedback. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -189,6 +189,7 @@ def __init__( | |
| self._connection_state = ConnectionState.CONN_DISCONNECTED | ||
| self._first_sid_future = asyncio.Future[str]() | ||
| self._local_participant: LocalParticipant | None = None | ||
| self._is_connecting: bool = False | ||
|
|
||
| # active incoming stream readers, keyed by FFI reader handle id | ||
| self._text_stream_readers: Dict[int, TextStreamReader] = {} | ||
|
|
@@ -209,10 +210,16 @@ async def sid(self) -> str: | |
|
|
||
| Returns: | ||
| str: The session ID of the room. | ||
|
|
||
| Raises: | ||
| RuntimeError: If the room is disconnected and no SID was received. | ||
| """ | ||
| if self._info.sid: | ||
| return self._info.sid | ||
|
|
||
| if self._connection_state == ConnectionState.CONN_DISCONNECTED and not self._is_connecting: | ||
| raise RuntimeError("cannot get sid of a disconnected room") | ||
|
|
||
| # This future is shared state for the Room. Caller-side timeouts, e.g. | ||
| # asyncio.wait_for(room.sid, ...), should not cancel it for future waiters. | ||
| return await asyncio.shield(self._first_sid_future) | ||
|
|
@@ -221,8 +228,16 @@ def _resolve_first_sid(self, sid: str) -> None: | |
| # The room SID can arrive through the initial connect result or later | ||
| # room update events. Resolve the first-SID waiter from any source that | ||
| # carries it so waiters do not depend on event ordering. | ||
| if sid and not self._first_sid_future.done(): | ||
| self._first_sid_future.set_result(sid) | ||
| if sid: | ||
| self._info.sid = sid | ||
| if not self._first_sid_future.done(): | ||
| self._first_sid_future.set_result(sid) | ||
|
|
||
| def _fail_first_sid(self, exc: BaseException) -> None: | ||
| if not self._first_sid_future.done(): | ||
| self._first_sid_future.set_exception(exc) | ||
| # Retrieve the exception to suppress unretrieved future warnings if unawaited | ||
| self._first_sid_future.exception() | ||
|
|
||
| @property | ||
| def local_participant(self) -> LocalParticipant: | ||
|
|
@@ -469,6 +484,9 @@ def on_participant_connected(participant): | |
| """ | ||
| self._server_url = url | ||
| self._token = token | ||
| self._is_connecting = True | ||
| if self._first_sid_future.done() and not self._info.sid: | ||
| self._first_sid_future = asyncio.Future[str]() | ||
| req = proto_ffi.FfiRequest() | ||
| req.connect.url = url | ||
| req.connect.token = token | ||
|
|
@@ -560,43 +578,55 @@ def on_participant_connected(participant): | |
| ), | ||
| ) | ||
|
|
||
| queue = FfiClient.instance.queue.subscribe() | ||
| try: | ||
| resp = FfiClient.instance.request(req) | ||
| cb: proto_ffi.FfiEvent = await queue.wait_for( | ||
| lambda e: e.connect.async_id == resp.connect.async_id | ||
| ) | ||
| finally: | ||
| FfiClient.instance.queue.unsubscribe(queue) | ||
|
|
||
| if cb.connect.error: | ||
| FfiClient.instance.queue.unsubscribe(self._ffi_queue) | ||
| raise ConnectError(cb.connect.error) | ||
|
|
||
| self._ffi_handle = FfiHandle(cb.connect.result.room.handle.id) | ||
| queue = FfiClient.instance.queue.subscribe() | ||
| try: | ||
| resp = FfiClient.instance.request(req) | ||
| cb: proto_ffi.FfiEvent = await queue.wait_for( | ||
| lambda e: e.connect.async_id == resp.connect.async_id | ||
| ) | ||
| finally: | ||
| FfiClient.instance.queue.unsubscribe(queue) | ||
|
|
||
| self._e2ee_manager = E2EEManager( | ||
| self._ffi_handle.handle, options.encryption or options.e2ee | ||
| ) | ||
| if cb.connect.error: | ||
| FfiClient.instance.queue.unsubscribe(self._ffi_queue) | ||
| raise ConnectError(cb.connect.error) | ||
|
|
||
| self._info = cb.connect.result.room.info | ||
| self._resolve_first_sid(self._info.sid) | ||
| self._connection_state = ConnectionState.CONN_CONNECTED | ||
| self._ffi_handle = FfiHandle(cb.connect.result.room.handle.id) | ||
|
|
||
| self._local_participant = LocalParticipant( | ||
| self._room_queue, cb.connect.result.local_participant | ||
| ) | ||
| self._e2ee_manager = E2EEManager( | ||
| self._ffi_handle.handle, options.encryption or options.e2ee | ||
| ) | ||
|
|
||
| for pt in cb.connect.result.participants: | ||
| rp = self._create_remote_participant(pt.participant) | ||
| self._info = cb.connect.result.room.info | ||
| self._resolve_first_sid(self._info.sid) | ||
| self._connection_state = ConnectionState.CONN_CONNECTED | ||
|
|
||
| # add the initial remote participant tracks | ||
| for owned_publication_info in pt.publications: | ||
| publication = RemoteTrackPublication(owned_publication_info) | ||
| rp._track_publications[publication.sid] = publication | ||
| self._local_participant = LocalParticipant( | ||
| self._room_queue, cb.connect.result.local_participant | ||
| ) | ||
|
|
||
| # start listening to room events | ||
| self._task = self._loop.create_task(self._listen_task()) | ||
| for pt in cb.connect.result.participants: | ||
| rp = self._create_remote_participant(pt.participant) | ||
|
|
||
| # add the initial remote participant tracks | ||
| for owned_publication_info in pt.publications: | ||
| publication = RemoteTrackPublication(owned_publication_info) | ||
| rp._track_publications[publication.sid] = publication | ||
|
|
||
| # start listening to room events | ||
| self._task = self._loop.create_task(self._listen_task()) | ||
| except BaseException as e: | ||
| if self._ffi_handle is None and hasattr(self, "_ffi_queue"): | ||
| FfiClient.instance.queue.unsubscribe(self._ffi_queue) | ||
| self._fail_first_sid( | ||
| RuntimeError("room connection was cancelled") | ||
| if isinstance(e, asyncio.CancelledError) | ||
| else e | ||
| ) | ||
| raise | ||
| finally: | ||
| self._is_connecting = False | ||
|
lukasIO marked this conversation as resolved.
Resolved
|
||
|
|
||
| # unsubscribe from the FFI queue once the listen task ends. | ||
| # disconnect() unsubscribes too, but it early-returns when the | ||
|
|
@@ -713,6 +743,7 @@ async def disconnect( | |
| if self._connection_state != ConnectionState.CONN_DISCONNECTED: | ||
| self.local_participant._info.disconnect_reason = reason | ||
| self._connection_state = ConnectionState.CONN_DISCONNECTED | ||
| self._fail_first_sid(RuntimeError("room disconnected before receiving sid")) | ||
| self.emit("connection_state_changed", self._connection_state) | ||
| self.emit("disconnected", reason) | ||
|
|
||
|
|
@@ -744,6 +775,7 @@ async def _listen_task(self) -> None: | |
| await self._drain_rpc_invocation_tasks() | ||
| self._error_stream_readers() | ||
| self._dispose_open_stream_writers() | ||
| self._fail_first_sid(RuntimeError("room disconnected before receiving sid")) | ||
|
|
||
| def _on_rpc_method_invocation(self, rpc_invocation: RpcMethodInvocationEvent) -> None: | ||
| if self._local_participant is None: | ||
|
|
@@ -770,9 +802,12 @@ def _on_room_event(self, event: proto_room.RoomEvent) -> None: | |
| self.emit("participant_connected", rparticipant) | ||
| elif which == "participant_disconnected": | ||
| identity = event.participant_disconnected.participant_identity | ||
| rparticipant = self._remote_participants.pop(identity) | ||
| rparticipant._info.disconnect_reason = event.participant_disconnected.disconnect_reason | ||
| self.emit("participant_disconnected", rparticipant) | ||
| if identity in self._remote_participants: | ||
| rparticipant = self._remote_participants.pop(identity) | ||
| rparticipant._info.disconnect_reason = ( | ||
| event.participant_disconnected.disconnect_reason | ||
| ) | ||
| self.emit("participant_disconnected", rparticipant) | ||
| elif which == "participant_active": | ||
| rp = self._retrieve_remote_participant(event.participant_active.participant_identity) | ||
| if rp: | ||
|
|
@@ -982,8 +1017,8 @@ def _on_room_event(self, event: proto_room.RoomEvent) -> None: | |
| # TODO: pass participant identity | ||
| for identity in event.active_speakers_changed.participant_identities: | ||
| participant = self._retrieve_participant(identity) | ||
| assert isinstance(participant, Participant) | ||
| speakers.append(participant) | ||
| if participant is not None: | ||
| speakers.append(participant) | ||
|
|
||
| self.emit("active_speakers_changed", speakers) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Unchanged speakers trigger duplicate notifications When speaker updates repeat, Learn moreThe room event carries a complete list of active speaker identities. Consecutive FFI events can contain the same list, but the event handler emits each one. Consumers then receive redundant notifications, including repeated notifications for an empty room. Example: Two successive updates both contain Recommended fix: Keep the previous speaker identities in Was this helpful? React with 👍 or 👎 to provide feedback. |
||
| elif which == "room_metadata_changed": | ||
|
|
@@ -1126,6 +1161,7 @@ def _on_room_event(self, event: proto_room.RoomEvent) -> None: | |
| self._connection_state = connection_state | ||
| self.emit("connection_state_changed", connection_state) | ||
| elif which == "disconnected": | ||
| self._fail_first_sid(RuntimeError("room disconnected before receiving sid")) | ||
| self.emit("disconnected", event.disconnected.reason) | ||
| elif which == "reconnecting": | ||
| self.emit("reconnecting") | ||
|
|
@@ -1243,8 +1279,12 @@ def _retrieve_remote_participant(self, identity: str) -> Optional[RemoteParticip | |
|
|
||
| def _retrieve_participant(self, identity: str) -> Optional[Participant]: | ||
| """Retrieve a local or remote participant by identity""" | ||
| if identity and identity == self.local_participant.identity: | ||
| return self.local_participant | ||
| if ( | ||
| identity | ||
| and self._local_participant is not None | ||
| and identity == self._local_participant.identity | ||
| ): | ||
| return self._local_participant | ||
|
|
||
| return self._retrieve_remote_participant(identity) | ||
|
|
||
|
|
@@ -1260,7 +1300,13 @@ def _create_remote_participant( | |
|
|
||
| def __repr__(self) -> str: | ||
| sid = "unknown" | ||
| if self._first_sid_future.done(): | ||
| if self._info.sid: | ||
| sid = self._info.sid | ||
| elif ( | ||
| self._first_sid_future.done() | ||
| and not self._first_sid_future.cancelled() | ||
| and self._first_sid_future.exception() is None | ||
| ): | ||
| sid = self._first_sid_future.result() | ||
|
|
||
| return f"rtc.Room(sid={sid}, name={self.name}, metadata={self.metadata}, connection_state={ConnectionState.Name(self._connection_state)})" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| # Copyright 2026 LiveKit, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import asyncio | ||
|
|
||
| import pytest | ||
|
|
||
| from livekit import rtc | ||
| from livekit.rtc import ConnectionState | ||
| from livekit.rtc._proto import ffi_pb2 as proto_ffi | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_room_sid_disconnected_raises_runtime_error() -> None: | ||
| room = rtc.Room() | ||
| with pytest.raises(RuntimeError, match="cannot get sid of a disconnected room"): | ||
| await room.sid | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_room_sid_resolution() -> None: | ||
| room = rtc.Room() | ||
| room._connection_state = ConnectionState.CONN_CONNECTED | ||
|
|
||
| async def resolve_soon() -> None: | ||
| await asyncio.sleep(0.01) | ||
| room._resolve_first_sid("RM_test123") | ||
|
|
||
| task = asyncio.create_task(resolve_soon()) | ||
| sid = await room.sid | ||
| await task | ||
| assert sid == "RM_test123" | ||
| assert "RM_test123" in repr(room) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_room_sid_failed_on_disconnect() -> None: | ||
| room = rtc.Room() | ||
| room._connection_state = ConnectionState.CONN_CONNECTED | ||
|
|
||
| async def wait_sid() -> None: | ||
| await room.sid | ||
|
|
||
| wait_task = asyncio.create_task(wait_sid()) | ||
| await asyncio.sleep(0.01) | ||
|
|
||
| # Room disconnects before receiving SID | ||
| room._fail_first_sid(RuntimeError("room disconnected before receiving sid")) | ||
|
|
||
| with pytest.raises(RuntimeError, match="room disconnected before receiving sid"): | ||
| await wait_task | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_connect_cancellation_fails_sid_future_and_cleans_up( | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| ) -> None: | ||
| room = rtc.Room() | ||
|
|
||
| # Mock FfiClient request to never answer connect response | ||
| def mock_request(req: proto_ffi.FfiRequest) -> proto_ffi.FfiResponse: | ||
| resp = proto_ffi.FfiResponse() | ||
| resp.connect.async_id = 9999 | ||
| return resp | ||
|
|
||
| monkeypatch.setattr(rtc._ffi_client.FfiClient.instance, "request", mock_request) | ||
|
|
||
| connect_task = asyncio.create_task(room.connect("ws://dummy", "dummy_token")) | ||
| await asyncio.sleep(0.01) | ||
|
|
||
| assert room._is_connecting | ||
|
|
||
| async def wait_sid() -> None: | ||
| await room.sid | ||
|
|
||
| sid_task = asyncio.create_task(wait_sid()) | ||
| await asyncio.sleep(0.01) | ||
|
|
||
| connect_task.cancel() | ||
| with pytest.raises(asyncio.CancelledError): | ||
| await connect_task | ||
|
|
||
| assert not room._is_connecting | ||
| with pytest.raises(RuntimeError, match="room connection was cancelled"): | ||
| await sid_task |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Disconnected speakers remain in listener state
When a speaking participant or room disconnects,
active_speakers_changedno longer reports the reduced or empty list. Listeners retain departed speakers until another speaker update arrives.(Refers to this code)
Learn more
Speaker listeners receive a complete speaker list, not incremental removal events. On a participant disconnect, the room removes the participant and emits
participant_disconnected, but no longer tells speaker listeners that their list changed. The same omission occurs on both room disconnect paths in disconnect and the disconnected event.Example: The last speaker is Alice. Alice disconnects, and a UI that updates speakers only from
active_speakers_changedcontinues to show Alice until another speaker update arrives.Recommended fix: Maintain current speakers and emit a reduced list when a speaking participant disconnects; emit an empty list on room disconnect when any speakers remain.
Was this helpful? React with 👍 or 👎 to provide feedback.