fix: try to handle startup BadMsgNotification #11

Retry to generate a new msg_id to send before raising BadMsgNotification.

Signed-off-by: wulan17 <wulan17@nusantararom.org>
This commit is contained in:
mrmissx 2023-06-30 19:20:03 +07:00 committed by wulan17
parent 02733d90f2
commit 5ba8cc48fc
No known key found for this signature in database
GPG key ID: 318CD6CD3A6AC0A5

View file

@ -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 <https://github.com/delivrance>
# Copyright (C) 2022-present Mayuri-Chan <https://github.com/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 <http://www.gnu.org/licenses/>.
# along with Pyrofork. If not, see <http://www.gnu.org/licenses/>.
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,