@@ -88,7 +88,7 @@ class Session:
8888 ACKS_THRESHOLD = 10
8989 PING_INTERVAL = 5
9090 RETRY_DELAY = 1
91- MAX_RESTART_DELAY = 30
91+ MAX_RESTART_DELAY = 10
9292 STORED_MSG_IDS_MAX_SIZE = 1000 * 2
9393 CRYPTO_EXECUTOR_WORKERS = 1
9494 MAX_CONSECUTIVE_IGNORED = 30
@@ -144,6 +144,7 @@ def __init__(
144144 self .fatal_error : Optional [BaseException ] = None
145145
146146 self ._consecutive_restarts : int = 0
147+ self ._restart_generation : int = 0
147148
148149 @property
149150 def _log_prefix (self ) -> str :
@@ -275,10 +276,15 @@ async def stop(self) -> None:
275276 await self ._invoke_handler (self .client .disconnect_handler )
276277
277278 async def restart (self ) -> None :
279+ # Capture generation BEFORE acquiring the lock (no await in between, so
280+ # no interleaving in asyncio). If another restart completes while we
281+ # wait for the lock, the generation will have advanced and we can skip.
282+ expected_gen = self ._restart_generation
283+
278284 async with self .restart_lock :
279- # Skip if another restart already completed while we waited for the lock
280- if self . _state == SessionState . STARTED :
281- log . debug ( "[%s] Session already restarted, skipping redundant restart" , self ._log_prefix )
285+ if self . _restart_generation != expected_gen :
286+ log . debug ( "[%s] Skipping redundant restart (generation %d -> %d)" ,
287+ self ._log_prefix , expected_gen , self . _restart_generation )
282288 return
283289
284290 if self .stored_msg_ids :
@@ -298,12 +304,23 @@ async def restart(self) -> None:
298304 await asyncio .sleep (delay )
299305
300306 self ._consecutive_restarts += 1
307+ self ._restart_generation += 1
301308
302309 self .session_id = os .urandom (8 )
303310 self .msg_factory = MsgFactory (self .client )
304311
305312 await self .start ()
306313
314+ # After lock released: sync update state so the server resumes pushing
315+ # updates on the new session. Only needed for the main session.
316+ if self .is_started .is_set () and not self .is_cdn and not self .is_media :
317+ try :
318+ await self .client .invoke (raw .functions .updates .GetState ())
319+ log .info ("[%s] Post-restart update state synced" , self ._log_prefix )
320+ except Exception as e :
321+ log .warning ("[%s] Post-restart update sync failed: %s - %s" ,
322+ self ._log_prefix , type (e ).__name__ , e )
323+
307324 def _fail_pending_results (self , error : BaseException ) -> None :
308325 for result in self .results .values ():
309326 if result .value is None :
0 commit comments