Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions livekit-rtc/livekit/rtc/track_publication.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ def set_subscribed(self, subscribed: bool) -> None:
req.set_subscribed.publication_handle = self._ffi_handle.handle
FfiClient.instance.request(req)

def set_enabled(self, enabled: bool) -> None:
"""Enable or disable media delivery for this subscribed remote track.

Unlike changing the subscription, this retains the subscription and
requests a media delivery change without renegotiation. It does not
change the publisher's mute state or affect other subscribers.
Call this after the track is subscribed. The request is not a guarantee
that in-flight media has drained or that resumed media has arrived.
"""
req = proto_ffi.FfiRequest()
req.enable_remote_track_publication.track_publication_handle = self._ffi_handle.handle
req.enable_remote_track_publication.enabled = enabled
FfiClient.instance.request(req)

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.

🔴 Track toggle crashes during unpublish

When a publisher removes a track during set_enabled, the native toggle can unwrap a track that no longer exists. The FFI panic callback terminates the Python process.

Learn more

The native request handler forwards the toggle to the Rust publication. The Rust method checks for an attached media track, checks whether that track is enabled, then reads the track again and unwraps it. Each lookup takes a separate read lock in track. A publisher can unpublish concurrently, and unpublish_track_inner clears the track. The native C bridge catches the resulting panic and forwards it to the Python callback, which sends SIGTERM.

Example: An agent pauses an incoming microphone while the other participant stops publishing it. If the unpublish clears the track after the enabled check but before the final lookup, the agent process exits instead of ignoring the toggle on the removed track.

Recommended fix: Fix the pinned Rust implementation to take one optional track snapshot, return if it is absent, and compare and update that snapshot with the requested value. Verify whether signaling also needs synchronization with detach. A Python subscribed check cannot close the native race.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +133 to +134

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.

🔴 Stale pause overrides track resume

Successive set_enabled calls spawn separate signaling tasks, so a pause can reach the server after its matching resume. The native toggle skips another resume because the local track already reads as enabled.

Learn more

The native toggle changes the local track synchronously but spawns each server update independently. The tasks run on a multi-threaded runtime. The signaling writer sends requests in channel arrival order, not in Python call order. If the earlier task reaches that channel last, the media server applies the older value. A repeat call requesting the current local value is a no-op.

Example: An agent calls set_enabled(False) and then set_enabled(True) while both updates are still being sent. If the resume task enqueues first, the server processes resume and then pause. The local track stays enabled, but incoming media stays paused.

Recommended fix: Serialize settings updates per publication in the native SDK, or coalesce pending updates into a single ordered sender that always transmits the newest desired settings last. Test two sequential API calls with the first send deliberately delayed.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +132 to +134

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.

🟡 Media toggles overwrite simulcast settings

On a simulcast track, set_enabled(True) overwrites the layer selected by set_video_quality with Low. Changing quality while paused also tells the media server to resume forwarding.

Learn more

Both operations send the same subscriber-settings message to the media server. The native enable callback fills in disabled but leaves quality at its protocol default, Low. The native quality callback fills in quality but leaves disabled at false. LiveKit Server's mergeSubscriberSettings preserves omitted FEC settings, not quality or disabled, and SubscribedTrack.applySettings uses the resulting settings for forwarding and layer selection.

Example: A subscriber selects High for a simulcast camera, pauses it, then resumes it. The resume request carries Low, so the server switches to the lowest layer. If the subscriber selects High during the pause instead, the settings request clears the server pause even though the local track stays disabled.

Recommended fix: Keep a single set of desired subscriber settings in the Rust SDK and send the full set on either update, preserving both the requested layer and whether forwarding is paused. Cover each call order with a server-backed simulcast test.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.


def set_video_quality(self, quality: "VideoQuality.ValueType") -> None:
"""For simulcasted video tracks, request a specific simulcast layer
from the server. Use one of `rtc.VideoQuality.VIDEO_QUALITY_LOW` (q),
Expand Down
37 changes: 37 additions & 0 deletions livekit-rtc/tests/test_remote_track_publication_enabled.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2026 LiveKit, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from types import SimpleNamespace
from unittest.mock import patch

import pytest

from livekit.rtc import RemoteTrackPublication


@pytest.mark.parametrize("enabled", [False, True])
def test_set_enabled_sends_publication_request(enabled: bool) -> None:
publication = object.__new__(RemoteTrackPublication)
publication._ffi_handle = SimpleNamespace(handle=123) # type: ignore[assignment]
publication._subscribed = True

with patch("livekit.rtc.track_publication.FfiClient") as ffi:
publication.set_enabled(enabled)

ffi.instance.request.assert_called_once()
request = ffi.instance.request.call_args.args[0]
assert request.WhichOneof("message") == "enable_remote_track_publication"
assert request.enable_remote_track_publication.track_publication_handle == 123
assert request.enable_remote_track_publication.enabled is enabled
assert publication.subscribed
Loading