mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-09-21 03:08:49 +03:00
Fix step_sender could deadlock
This commit is contained in:
parent
94048d9102
commit
26c32f31d2
|
@ -219,6 +219,7 @@ class Client:
|
||||||
|
|
||||||
self._sender: Optional[Sender] = None
|
self._sender: Optional[Sender] = None
|
||||||
self._sender_lock = asyncio.Lock()
|
self._sender_lock = asyncio.Lock()
|
||||||
|
self._sender_lock_flag = False
|
||||||
if isinstance(session, Storage):
|
if isinstance(session, Storage):
|
||||||
self._storage = session
|
self._storage = session
|
||||||
elif session is None:
|
elif session is None:
|
||||||
|
|
|
@ -268,19 +268,23 @@ async def step(client: Client) -> None:
|
||||||
|
|
||||||
|
|
||||||
async def step_sender(client: Client, sender: Sender, lock: asyncio.Lock) -> None:
|
async def step_sender(client: Client, sender: Sender, lock: asyncio.Lock) -> None:
|
||||||
if lock.locked():
|
flag = client._sender_lock_flag
|
||||||
async with lock:
|
async with lock:
|
||||||
pass
|
if client._sender_lock_flag != flag:
|
||||||
else:
|
# different task already received an item from the network
|
||||||
async with lock:
|
return
|
||||||
try:
|
# current task is responsible for receiving
|
||||||
updates = await sender.step()
|
# toggle the flag so any other task that comes after does not run again
|
||||||
except ConnectionError:
|
client._sender_lock_flag = not client._sender_lock_flag
|
||||||
if client.connected:
|
|
||||||
raise
|
try:
|
||||||
else:
|
updates = await sender.step()
|
||||||
# disconnect was called, so the socket returning 0 bytes is expected
|
except ConnectionError:
|
||||||
return
|
if client.connected:
|
||||||
|
raise
|
||||||
|
else:
|
||||||
|
# disconnect was called, so the socket returning 0 bytes is expected
|
||||||
|
return
|
||||||
|
|
||||||
process_socket_updates(client, updates)
|
process_socket_updates(client, updates)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user