Skip to content

Commit edb10c6

Browse files
fix(session): prevent race condition during connection cleanup
Reorder the stop() method to cancel recv_task BEFORE closing the connection. Previously, closing the connection first would set asyncio transport callbacks to None while recv_worker() might still be reading, causing 'TypeError: NoneType object is not callable' in _SelectorSocketTransport._read_ready().
1 parent 94f2b04 commit edb10c6

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

hydrogram/session/session.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ async def stop(self):
183183

184184
self.ping_task_event.clear()
185185

186-
if self.connection:
187-
await self.connection.close()
188-
186+
# Cancel recv_task first to avoid race condition with connection close
187+
# If connection closes before recv_task finishes, asyncio transport callbacks
188+
# can become None while still being invoked, causing TypeError
189189
if self.recv_task and not self.recv_task.done():
190190
self.recv_task.cancel()
191191

@@ -194,6 +194,9 @@ async def stop(self):
194194

195195
self.recv_task = None
196196

197+
if self.connection:
198+
await self.connection.close()
199+
197200
if not self.is_media and callable(self.client.disconnect_handler):
198201
try:
199202
await self.client.disconnect_handler(self.client)

0 commit comments

Comments
 (0)