Skip to content

Commit a522e65

Browse files
committed
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.
1 parent 678a064 commit a522e65

4 files changed

Lines changed: 864 additions & 868 deletions

File tree

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ repos:
2020
- id: check-yaml
2121

2222
- repo: https://github.com/astral-sh/ruff-pre-commit
23-
rev: v0.11.6
23+
rev: v0.11.7
2424
hooks:
2525
- id: ruff-format
2626
- id: ruff

hydrogram/session/session.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@ async def stop(self):
185185
await self.connection.close()
186186

187187
if self.recv_task:
188-
await self.recv_task
188+
self.recv_task.cancel()
189+
with contextlib.suppress(asyncio.CancelledError):
190+
await self.recv_task
189191

190192
if not self.is_media and callable(self.client.disconnect_handler):
191193
try:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ build-backend = "hatchling.build"
5353
[tool.uv]
5454
managed = true
5555
dev-dependencies = [
56-
"ruff>=0.11.6",
56+
"ruff>=0.11.7",
5757
"pytest>=7.4.3",
5858
"pytest-asyncio>=0.23.2",
5959
"pytest-cov>=4.1.0",

0 commit comments

Comments
 (0)