Skip to content

Commit 4ba6f9a

Browse files
Review changes
1 parent 677d98e commit 4ba6f9a

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

examples/transcription/transcription/transcription.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,3 @@ async def _recv_loop(self, session: AsyncSession):
5959

6060
if acc:
6161
self._on_text(acc)
62-
63-
return session

fishjam/agent/agent.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def __init__(self, id: str, token: str, fishjam_url: str):
4444
self._socket_url = f"{fishjam_url}/socket/agent/websocket".replace("http", "ws")
4545
self._token = token
4646
self._msg_loop: asyncio.Task[None] | None = None
47+
self._end_event = asyncio.Event()
4748

4849
@functools.singledispatch
4950
def _message_handler(content: Any) -> None:
@@ -75,24 +76,29 @@ async def connect(self):
7576

7677
websocket = await client.connect(self._socket_url)
7778
await self._authenticate(websocket)
78-
self._msg_loop = asyncio.create_task(self._recv_loop(websocket))
79+
80+
task = asyncio.create_task(self._recv_loop(websocket))
81+
82+
self._msg_loop = task
7983

8084
async def disconnect(self, code: CloseCode = CloseCode.NORMAL_CLOSURE):
8185
"""
8286
Disconnect the agent from Fishjam.
8387
8488
Does nothing if already disconnected.
8589
"""
86-
if not self._msg_loop:
90+
if (task := self._msg_loop) is None:
8791
return
8892

89-
with suppress(TimeoutError):
90-
self._msg_loop.cancel(code)
91-
async with asyncio.timeout(0):
92-
await self._msg_loop
93+
event = self._end_event
9394

95+
self._end_event = asyncio.Event()
9496
self._msg_loop = None
9597

98+
task.add_done_callback(lambda _t: event.set())
99+
if task.cancel(code):
100+
await event.wait()
101+
96102
async def __aenter__(self):
97103
await self.connect()
98104
return self

0 commit comments

Comments
 (0)