fix(rtc): guard remote track event handlers against racing disconnect#758
Open
AKomplished-bug wants to merge 1 commit into
Open
Conversation
AKomplished-bug
requested review from
cloudwebrtc,
lukasIO and
xianshijing-lk
as code owners
July 18, 2026 08:16
AKomplished-bug
force-pushed
the
fix/track-event-keyerror-racing-disconnect
branch
2 times, most recently
from
July 18, 2026 09:36
0ea9b1d to
5208d5f
Compare
track_published, track_subscribed, track_unsubscribed and track_subscription_failed looked up the remote participant (and publication) with an unguarded dict access, raising KeyError when the participant had already been removed by a racing participant_disconnected or unpublish. This mirrors the defensive lookup already applied to track_unpublished in livekit#743, skipping the emit with a debug log when the entry is gone instead of letting _listen_task log a KeyError.
AKomplished-bug
force-pushed
the
fix/track-event-keyerror-racing-disconnect
branch
from
July 18, 2026 09:41
5208d5f to
f719a91
Compare
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.
Fixes #757
Problem
Room._on_room_eventraisesKeyErrorin thetrack_subscribedhandler (and its siblings) when the remote participant has already been removed fromself._remote_participantsby a racingparticipant_disconnectedor a duplicate event:It is swallowed by the
except Exception: logging.exception(...)in_listen_task, so it does not break the session, but it is logged as an error (and reported to error trackers as noise). It shows up most often with SIP participants, whose connect/disconnect ordering is racier than typical WebRTC clients.Fix
#743 already made
track_unpublisheddefensive for exactly this race. This PR applies the same pattern to the remaining handlers that still used an unguardedself._remote_participants[identity]:track_publishedtrack_subscribedtrack_unsubscribedtrack_subscription_failedEach now looks the participant (and, where applicable, the publication) up with
.get(...)and skips the emit with alogger.debugwhen the entry is already gone, matching the wording and behaviour of the existingtrack_unpublishedhandler.No behavioural change on the happy path; only the racing-disconnect case is affected.