From 45f866c3353aebaa7a1f137afac323ade886bdbc Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Wed, 18 Oct 2023 20:31:09 +0200 Subject: [PATCH] Fix handling of bad_msg 16 and 17 --- .../src/telethon/_impl/mtproto/mtp/encrypted.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/client/src/telethon/_impl/mtproto/mtp/encrypted.py b/client/src/telethon/_impl/mtproto/mtp/encrypted.py index f49bd037..5cc923f4 100644 --- a/client/src/telethon/_impl/mtproto/mtp/encrypted.py +++ b/client/src/telethon/_impl/mtproto/mtp/encrypted.py @@ -149,6 +149,7 @@ class Encrypted(Mtp): now = time.time() correct = msg_id >> 32 self._time_offset = correct - int(now) + self._last_msg_id = 0 def _adjusted_now(self) -> float: return time.time() + self._time_offset @@ -288,20 +289,22 @@ class Encrypted(Mtp): exc = BadMessage(code=bad_msg.error_code) bad_msg_id = bad_msg.bad_msg_id - if self._out_pending_ack[bad_msg_id] is None: + if bad_msg_id in self._out_pending_ack: + bad_msg_ids = [bad_msg.bad_msg_id] + else: # Search bad_msg_id in containers instead. - # Make a new list since pending ack needs to be mutated after. bad_msg_ids = [ m for m, c in self._out_pending_ack.items() if bad_msg_id == c ] if not bad_msg_ids: raise KeyError(f"bad_msg for unknown msg_id: {bad_msg_id}") - for bad_msg_id in bad_msg_id: + for bad_msg_id in bad_msg_ids: + if bad_msg_id == self._salt_request_msg_id: + # Response to internal request, do not propagate. + self._salt_request_msg_id = None + else: self._rpc_results.append((MsgId(bad_msg_id), exc)) - del self._out_pending_ack[bad_msg_id] - else: - self._rpc_results.append((MsgId(bad_msg_id), exc)) del self._out_pending_ack[bad_msg_id] if isinstance(bad_msg, BadServerSalt) and self._get_current_salt() == 0: @@ -316,7 +319,7 @@ class Encrypted(Mtp): ) ) self._salt_request_msg_id = None - elif bad_msg.error_code not in (16, 17): + elif bad_msg.error_code in (16, 17): self._correct_time_offset(message.msg_id) elif bad_msg.error_code in (32, 33): self._reset_session()