fix(session): prevent task cancellation race condition in stop method

The fix properly cancels the recv_task and suppresses CancelledError when awaiting
it during session shutdown. This resolves the "read() called while another
coroutine is already waiting for incoming data" RuntimeError that occurred when
stopping sessions during reconnection attempts.

Signed-off-by: wulan17 <wulan17@komodos.id>
This commit is contained in:
Hitalo M. 2025-04-30 18:33:48 -03:00 committed by wulan17
parent 01e7717e52
commit b8028541c9
No known key found for this signature in database
GPG key ID: 737814D4B5FF0420

View file

@ -19,6 +19,7 @@
import asyncio import asyncio
import bisect import bisect
import contextlib
import logging import logging
import os import os
from datetime import datetime, timedelta from datetime import datetime, timedelta
@ -179,7 +180,9 @@ class Session:
await self.connection.close() await self.connection.close()
if self.recv_task: if self.recv_task:
await self.recv_task self.recv_task.cancel()
with contextlib.suppress(asyncio.CancelledError):
await self.recv_task
if not self.is_media and callable(self.client.disconnect_handler): if not self.is_media and callable(self.client.disconnect_handler):
try: try: