Skip to content

Commit 943ae9f

Browse files
authored
Release unfinished events when a BroadcastQueue subscriber is removed (#846)
1 parent 082e83c commit 943ae9f

2 files changed

Lines changed: 115 additions & 0 deletions

File tree

‎livekit-rtc/livekit/rtc/_utils.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,19 @@ async def wait_for(self, fnc: Callable[[T], bool]) -> T:
113113

114114
self.task_done()
115115

116+
def release(self) -> None:
117+
"""Mark every unfinished item as done.
118+
119+
Lets a ``join()`` that is already waiting on this queue return once there
120+
is no consumer left for it.
121+
"""
122+
while True:
123+
try:
124+
self.task_done()
125+
except ValueError:
126+
# task_done() raises once every item is accounted for
127+
return
128+
116129

117130
class BroadcastQueue(Generic[T]):
118131
"""Queue with multiple subscribers."""
@@ -135,6 +148,10 @@ def subscribe(self) -> Queue[T]:
135148

136149
def unsubscribe(self, queue: Queue[T]) -> None:
137150
self._subscribers.remove(queue)
151+
# A join() may already hold this queue. Events that are still queued, or that
152+
# the subscriber took but never finished, have no consumer anymore, so
153+
# release them instead of leaving the joiner waiting forever.
154+
queue.release()
138155

139156
async def join(self) -> None:
140157
async with self._lock:
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
"""Unit tests for BroadcastQueue subscriber removal.
2+
3+
``Room._listen_task`` calls ``BroadcastQueue.join()`` after every event, so a
4+
subscriber that goes away (for example a cancelled ``publish_track``) must not
5+
leave that join waiting on events nobody will finish.
6+
"""
7+
8+
from __future__ import annotations
9+
10+
import asyncio
11+
12+
import pytest
13+
14+
from livekit.rtc._utils import BroadcastQueue
15+
16+
17+
async def _join_finishes(queue: BroadcastQueue, timeout: float = 1.0) -> bool:
18+
try:
19+
await asyncio.wait_for(queue.join(), timeout)
20+
except asyncio.TimeoutError:
21+
return False
22+
return True
23+
24+
25+
@pytest.mark.asyncio
26+
async def test_join_waits_for_a_subscriber_that_has_not_finished() -> None:
27+
broadcast: BroadcastQueue[int] = BroadcastQueue()
28+
subscriber = broadcast.subscribe()
29+
broadcast.put_nowait(1)
30+
31+
assert not await _join_finishes(broadcast, timeout=0.1)
32+
33+
await subscriber.get()
34+
subscriber.task_done()
35+
assert await _join_finishes(broadcast)
36+
37+
38+
@pytest.mark.asyncio
39+
async def test_unsubscribe_releases_a_join_waiting_on_a_queued_event() -> None:
40+
broadcast: BroadcastQueue[int] = BroadcastQueue()
41+
subscriber = broadcast.subscribe()
42+
broadcast.put_nowait(1)
43+
44+
joiner = asyncio.create_task(broadcast.join())
45+
await asyncio.sleep(0)
46+
assert not joiner.done()
47+
48+
broadcast.unsubscribe(subscriber)
49+
50+
await asyncio.wait_for(joiner, 1.0)
51+
assert broadcast.len_subscribers() == 0
52+
53+
54+
@pytest.mark.asyncio
55+
async def test_unsubscribe_releases_a_join_waiting_on_an_event_that_was_taken() -> None:
56+
broadcast: BroadcastQueue[int] = BroadcastQueue()
57+
subscriber = broadcast.subscribe()
58+
broadcast.put_nowait(1)
59+
await subscriber.get() # taken, but the subscriber never calls task_done()
60+
61+
joiner = asyncio.create_task(broadcast.join())
62+
await asyncio.sleep(0)
63+
assert not joiner.done()
64+
65+
broadcast.unsubscribe(subscriber)
66+
67+
await asyncio.wait_for(joiner, 1.0)
68+
69+
70+
@pytest.mark.asyncio
71+
async def test_unsubscribe_keeps_other_subscribers_in_the_join() -> None:
72+
broadcast: BroadcastQueue[int] = BroadcastQueue()
73+
gone = broadcast.subscribe()
74+
staying = broadcast.subscribe()
75+
broadcast.put_nowait(1)
76+
77+
joiner = asyncio.create_task(broadcast.join())
78+
await asyncio.sleep(0)
79+
broadcast.unsubscribe(gone)
80+
await asyncio.sleep(0.05)
81+
assert not joiner.done()
82+
83+
await staying.get()
84+
staying.task_done()
85+
await asyncio.wait_for(joiner, 1.0)
86+
87+
88+
@pytest.mark.asyncio
89+
async def test_unsubscribe_after_every_event_is_finished_is_a_no_op() -> None:
90+
broadcast: BroadcastQueue[int] = BroadcastQueue()
91+
subscriber = broadcast.subscribe()
92+
broadcast.put_nowait(1)
93+
await subscriber.get()
94+
subscriber.task_done()
95+
96+
broadcast.unsubscribe(subscriber)
97+
98+
assert await _join_finishes(broadcast)

0 commit comments

Comments
 (0)