From 5ba8cc48fc99ba6398f036b6108af001391e4e6b Mon Sep 17 00:00:00 2001 From: mrmissx Date: Fri, 30 Jun 2023 19:20:03 +0700 Subject: [PATCH] fix: try to handle startup BadMsgNotification #11 Retry to generate a new msg_id to send before raising BadMsgNotification. Signed-off-by: wulan17 --- pyrogram/session/session.py | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/pyrogram/session/session.py b/pyrogram/session/session.py index 0ed967a1..99c7a14d 100644 --- a/pyrogram/session/session.py +++ b/pyrogram/session/session.py @@ -1,20 +1,21 @@ -# Pyrogram - Telegram MTProto API Client Library for Python +# Pyrofork - Telegram MTProto API Client Library for Python # Copyright (C) 2017-present Dan +# Copyright (C) 2022-present Mayuri-Chan # -# This file is part of Pyrogram. +# This file is part of Pyrofork. # -# Pyrogram is free software: you can redistribute it and/or modify +# Pyrofork is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published # by the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # -# Pyrogram is distributed in the hope that it will be useful, +# Pyrofork is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License -# along with Pyrogram. If not, see . +# along with Pyrofork. If not, see . import asyncio import bisect @@ -314,7 +315,7 @@ class Session: log.info("NetworkTask stopped") - async def send(self, data: TLObject, wait_response: bool = True, timeout: float = WAIT_TIMEOUT): + async def send(self, data: TLObject, wait_response: bool = True, timeout: float = WAIT_TIMEOUT, retry: int = 0): message = self.msg_factory(data) msg_id = message.msg_id @@ -357,7 +358,11 @@ class Session: RPCError.raise_it(result, type(data)) if isinstance(result, raw.types.BadMsgNotification): - log.warning("%s: %s", BadMsgNotification.__name__, BadMsgNotification(result.error_code)) + if retry > 1: + raise BadMsgNotification(result.error_code) + + self._handle_bad_notification() + await self.send(data, wait_response, timeout, retry + 1) if isinstance(result, raw.types.BadServerSalt): self.salt = result.new_server_salt @@ -365,6 +370,13 @@ class Session: return result + def _handle_bad_notification(self): + new_msg_id = MsgId() + if self.stored_msg_ids[len(self.stored_msg_ids)-1] >= new_msg_id: + new_msg_id = self.stored_msg_ids[len(self.stored_msg_ids)-1] + 4 + log.debug("Changing msg_id old=%s new=%s", self.stored_msg_ids[len(self.stored_msg_ids)-1], new_msg_id) + self.stored_msg_ids[len(self.stored_msg_ids)-1] = new_msg_id + async def invoke( self, query: TLObject,