Commit 2dbb047
fix(rtc): avoid KeyError in local_track_unpublished handler during teardown
Room._on_room_event did an unchecked
`self.local_participant.track_publications[sid]` lookup on the
`local_track_unpublished` branch. LocalParticipant.unpublish_track
removes the publication from `_track_publications` when its FFI async
response is processed, and that response races the `local_track_unpublished`
room event. When the response is handled first, the SID is already gone and
the handler raises KeyError, which _listen_task logs as a spurious ERROR
with a full traceback on every affected disconnect cleanup.
Pop the publication defensively in the handler and only emit when it is
still tracked, mirroring the remote `track_unpublished` and
`local_track_republished` handlers. This also folds removal into the
handler, fixing a latent leak where a server-forced unpublish (no
unpublish_track call) left the publication in the dict.
Because the handler now owns removal, the common (non-racing) ordering is
that the room event is processed before unpublish_track's pop. Make
unpublish_track's pop tolerant (`pop(track_sid, None)` + guarded
`_track = None`) so it does not raise a new KeyError once the handler has
already removed the publication.
Fixes #681
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent b15518a commit 2dbb047
2 files changed
Lines changed: 20 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
799 | 799 | | |
800 | 800 | | |
801 | 801 | | |
802 | | - | |
803 | | - | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
804 | 808 | | |
805 | 809 | | |
806 | 810 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
735 | 735 | | |
736 | 736 | | |
737 | 737 | | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
738 | 745 | | |
739 | | - | |
740 | | - | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
741 | 753 | | |
742 | 754 | | |
743 | 755 | | |
| |||
0 commit comments