Skip to content

Commit e1dfbe9

Browse files
Clean up late LiveKit joins and reject closed-session traffic
Signed-off-by: Juan Sepulveda <jsepulveda@nvidia.com>
1 parent 7c09b0f commit e1dfbe9

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

‎src/plugins/transport_livekit/isaac_transport_livekit/connection.py‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from livekit import api, rtc
1212

1313
logger = logging.getLogger("isaaccapture.remote_assistance.connection")
14+
CONNECT_TIMEOUT_S = 15
15+
_pending_cleanup = set()
1416

1517

1618
def token_for(settings: dict, identity: str, *, publish: bool = True) -> str:
@@ -32,6 +34,8 @@ def token_for(settings: dict, identity: str, *, publish: bool = True) -> str:
3234
)
3335
.to_jwt()
3436
)
37+
if urlparse(settings["url"]).scheme not in ("wss", "https"):
38+
raise ValueError("Production credentials require a wss:// or https:// URL")
3539
env = settings.get("token_env", "LIVEKIT_TOKEN")
3640
token = os.environ.get(env)
3741
if not token:
@@ -41,18 +45,35 @@ def token_for(settings: dict, identity: str, *, publish: bool = True) -> str:
4145

4246
async def connect(room: rtc.Room, settings: dict, identity: str, *, publish=True):
4347
token = token_for(settings, identity, publish=publish)
44-
async with asyncio.timeout(15):
45-
await room.connect(
48+
joining = asyncio.create_task(
49+
room.connect(
4650
settings["url"],
4751
token,
4852
options=rtc.RoomOptions(auto_subscribe=False, dynacast=False),
4953
)
54+
)
55+
try:
56+
await asyncio.wait_for(asyncio.shield(joining), CONNECT_TIMEOUT_S)
57+
except BaseException:
58+
# The SDK assigns its handle late; cancellation cannot safely reclaim the room.
59+
cleanup = asyncio.create_task(_disconnect_late_room(joining, room))
60+
_pending_cleanup.add(cleanup)
61+
cleanup.add_done_callback(_pending_cleanup.discard)
62+
raise
5063
if room.local_participant.identity != identity or room.name != settings["room"]:
5164
await room.disconnect()
5265
raise ValueError("Token identity/room does not match the configured endpoint")
5366
logger.info("Joined room %s as %s", room.name, identity)
5467

5568

69+
async def _disconnect_late_room(joining, room):
70+
try:
71+
await joining
72+
await disconnect(room)
73+
except Exception:
74+
logger.debug("Late LiveKit connection cleanup failed", exc_info=True)
75+
76+
5677
async def disconnect(room: rtc.Room):
5778
try:
5879
async with asyncio.timeout(5):

‎src/plugins/transport_livekit/isaac_transport_livekit/data.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ def __init__(self, settings, identity, peer, topics, on_message):
5252
@property
5353
def ready(self):
5454
return (
55-
self.room.isconnected()
55+
not self.closed.is_set()
56+
and self.room.isconnected()
5657
and self._peer_lease is not None
5758
and time.monotonic() - self._peer_lease_at < self.receiver.max_age_s
5859
)
@@ -70,6 +71,8 @@ def _disconnected(self, *_):
7071
self.closed.set()
7172

7273
def _received(self, packet):
74+
if self.closed.is_set():
75+
return
7376
if packet.participant is None or packet.participant.identity != self.peer:
7477
self.stats["wrong_peer"] += 1
7578
return

0 commit comments

Comments
 (0)