Skip to content

fix(rtc): handle repeated local track subscription events - #771

Merged
lukasIO merged 1 commit into
livekit:mainfrom
swayamg20:fix/770-duplicate-local-track-subscribed
Jul 31, 2026
Merged

fix(rtc): handle repeated local track subscription events#771
lukasIO merged 1 commit into
livekit:mainfrom
swayamg20:fix/770-duplicate-local-track-subscribed

Conversation

@swayamg20

Copy link
Copy Markdown
Contributor

Fixes #770

Problem

During a full reconnect, the server can resend local_track_subscribed for a reused LocalTrackPublication. Its _first_subscription future is one-shot and already resolved, so calling set_result(None) again raises asyncio.InvalidStateError. _listen_task catches the exception, logs an error, and drops the repeated event.

Fix

Only resolve _first_subscription while it is pending. The handler still emits local_track_subscribed for every event, preserving callback behavior across reconnects.

The regression test dispatches the same event twice and verifies both branches:

  • the first event resolves wait_for_subscription();
  • the repeated event does not raise and is still delivered to listeners.

Testing

  • Regression test fails with InvalidStateError on the unguarded handler and passes with this change.
  • MACOSX_DEPLOYMENT_TARGET=11.0 uv run pytest livekit-rtc/tests -q — 33 passed, 14 skipped.
  • Ruff lint and format checks pass across the maintained Python packages and tests.
  • Mypy passes across livekit-protocol, livekit-api, and livekit-rtc/livekit.
  • make build-wheel PACKAGE=livekit-rtc succeeds.

The full root test command also reached 48 passed and 30 skipped. Three audio-fixture tests could not run locally because Git LFS was unavailable and the WAV fixtures remained pointer files; CI is configured to fetch those LFS assets.

@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

@cloudwebrtc cloudwebrtc 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.

lgtm

@lukasIO
lukasIO merged commit 1add524 into livekit:main Jul 31, 2026
45 of 47 checks passed
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.

LiveKit Python SDK - unhandled InvalidStateError in _on_room_event silently drops local_track_subscribed after a full reconnect

3 participants