Skip to content

Commit a9a28f0

Browse files
authored
avoid KeyError when track is already unpublished (#743)
1 parent 35dc7eb commit a9a28f0

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

livekit-rtc/livekit/rtc/room.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from ._proto.rpc_pb2 import RpcMethodInvocationEvent
3434
from ._utils import BroadcastQueue
3535
from .e2ee import E2EEManager, E2EEOptions
36+
from .log import logger
3637
from .participant import (
3738
LocalParticipant,
3839
Participant,
@@ -821,11 +822,22 @@ def _on_room_event(self, event: proto_room.RoomEvent) -> None:
821822
rparticipant._track_publications[rpublication.sid] = rpublication
822823
self.emit("track_published", rpublication, rparticipant)
823824
elif which == "track_unpublished":
824-
rparticipant = self._remote_participants[event.track_unpublished.participant_identity]
825-
rpublication = rparticipant._track_publications.pop(
826-
event.track_unpublished.publication_sid
827-
)
828-
self.emit("track_unpublished", rpublication, rparticipant)
825+
# The participant or publication may already have been removed by a
826+
# racing disconnect or a duplicate event, so both lookups are done
827+
# defensively and the emit is skipped when the entry is gone,
828+
# mirroring the local_track_unpublished handler, instead of raising a
829+
# KeyError that _listen_task logs as an error.
830+
identity = event.track_unpublished.participant_identity
831+
sid = event.track_unpublished.publication_sid
832+
rp = self._remote_participants.get(identity)
833+
if rp is not None:
834+
rpub = rp._track_publications.pop(sid, None)
835+
if rpub is not None:
836+
self.emit("track_unpublished", rpub, rp)
837+
else:
838+
logger.debug("track_unpublished for untracked publication sid %s", sid)
839+
else:
840+
logger.debug("track_unpublished for untracked participant %s", identity)
829841
elif which == "track_subscribed":
830842
owned_track_info = event.track_subscribed.track
831843
track_info = owned_track_info.info

0 commit comments

Comments
 (0)