Skip to content

fix(rtc): guard remote track event handlers against racing disconnect#758

Open
AKomplished-bug wants to merge 1 commit into
livekit:mainfrom
AKomplished-bug:fix/track-event-keyerror-racing-disconnect
Open

fix(rtc): guard remote track event handlers against racing disconnect#758
AKomplished-bug wants to merge 1 commit into
livekit:mainfrom
AKomplished-bug:fix/track-event-keyerror-racing-disconnect

Conversation

@AKomplished-bug

Copy link
Copy Markdown

Fixes #757

Problem

Room._on_room_event raises KeyError in the track_subscribed handler (and its siblings) when the remote participant has already been removed from self._remote_participants by a racing participant_disconnected or a duplicate event:

KeyError: 'sip-caller-+91XXXXXXXXXX'
  File ".../livekit/rtc/room.py", in _on_room_event
    rparticipant = self._remote_participants[event.track_subscribed.participant_identity]

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_unpublished defensive for exactly this race. This PR applies the same pattern to the remaining handlers that still used an unguarded self._remote_participants[identity]:

  • track_published
  • track_subscribed
  • track_unsubscribed
  • track_subscription_failed

Each now looks the participant (and, where applicable, the publication) up with .get(...) and skips the emit with a logger.debug when the entry is already gone, matching the wording and behaviour of the existing track_unpublished handler.

No behavioural change on the happy path; only the racing-disconnect case is affected.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 1 additional finding.

Open in Devin Review

@AKomplished-bug
AKomplished-bug force-pushed the fix/track-event-keyerror-racing-disconnect branch 2 times, most recently from 0ea9b1d to 5208d5f Compare July 18, 2026 09:36
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
AKomplished-bug force-pushed the fix/track-event-keyerror-racing-disconnect branch from 5208d5f to f719a91 Compare July 18, 2026 09:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KeyError in _on_room_event for track_subscribed when participant already removed by a racing disconnect

1 participant