@@ -44,11 +44,11 @@ def __init__(self):
4444
4545
4646class Session :
47- START_TIMEOUT = 1
47+ START_TIMEOUT = 5
4848 WAIT_TIMEOUT = 15
4949 SLEEP_THRESHOLD = 10
50- MAX_RETRIES = 5
51- ACKS_THRESHOLD = 8
50+ MAX_RETRIES = 10
51+ ACKS_THRESHOLD = 10
5252 PING_INTERVAL = 5
5353
5454 def __init__ (
@@ -156,14 +156,11 @@ async def stop(self):
156156
157157 self .ping_task_event .clear ()
158158
159- self .connection .close ()
159+ await self .connection .close ()
160160
161161 if self .recv_task :
162162 await self .recv_task
163163
164- for i in self .results .values ():
165- i .event .set ()
166-
167164 if not self .is_media and callable (self .client .disconnect_handler ):
168165 try :
169166 await self .client .disconnect_handler (self .client )
@@ -189,6 +186,7 @@ async def handle_packet(self, packet):
189186 )
190187 except SecurityCheckMismatch as e :
191188 log .warning ("Discarding packet: %s" , e )
189+ await self .connection .close ()
192190 return
193191
194192 messages = (
@@ -284,9 +282,6 @@ async def send(self, data: TLObject, wait_response: bool = True, timeout: float
284282 message = self .msg_factory (data )
285283 msg_id = message .msg_id
286284
287- if wait_response :
288- self .results [msg_id ] = Result ()
289-
290285 log .debug ("Sent: %s" , message )
291286
292287 payload = await self .loop .run_in_executor (
@@ -299,34 +294,35 @@ async def send(self, data: TLObject, wait_response: bool = True, timeout: float
299294 self .auth_key_id
300295 )
301296
302- try :
303- await self .connection .send (payload )
304- except OSError as e :
305- self .results .pop (msg_id , None )
306- raise e
297+ await self .connection .send (payload )
307298
308299 if wait_response :
300+ self .results [msg_id ] = Result ()
301+
309302 try :
310303 await asyncio .wait_for (self .results [msg_id ].event .wait (), timeout )
311304 except asyncio .TimeoutError :
312305 pass
313- finally :
314- result = self .results .pop (msg_id ).value
306+
307+ result = self .results .pop (msg_id ).value
315308
316309 if result is None :
317310 raise TimeoutError ("Request timed out" )
318- elif isinstance (result , raw .types .RpcError ):
311+
312+ if isinstance (result , raw .types .RpcError ):
319313 if isinstance (data , (raw .functions .InvokeWithoutUpdates , raw .functions .InvokeWithTakeout )):
320314 data = data .query
321315
322316 RPCError .raise_it (result , type (data ))
323- elif isinstance (result , raw .types .BadMsgNotification ):
317+
318+ if isinstance (result , raw .types .BadMsgNotification ):
324319 raise BadMsgNotification (result .error_code )
325- elif isinstance (result , raw .types .BadServerSalt ):
320+
321+ if isinstance (result , raw .types .BadServerSalt ):
326322 self .salt = result .new_server_salt
327323 return await self .send (data , wait_response , timeout )
328- else :
329- return result
324+
325+ return result
330326
331327 async def invoke (
332328 self ,
0 commit comments