From ae028ab4b67b2c7aab9726a48aeabb0b5432be91 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 25 Dec 2022 14:55:40 +0100 Subject: [PATCH] Switch back to local system time synchronization perf_counter will stop counting when the system goes to sleep, causing the generation of invalid message ids after waking up which in turn put the client into a never ending reconnecting loop due to check mismatches caused by the time not being synced anymore. It's also unclear whether perf_counter stays in sync during long runs. --- pyrogram/session/internals/msg_id.py | 20 +++++--------------- pyrogram/session/session.py | 3 --- 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/pyrogram/session/internals/msg_id.py b/pyrogram/session/internals/msg_id.py index 871a10b6..58e3087c 100644 --- a/pyrogram/session/internals/msg_id.py +++ b/pyrogram/session/internals/msg_id.py @@ -17,29 +17,19 @@ # along with Pyrogram. If not, see . import logging -from datetime import datetime -from time import perf_counter +import time log = logging.getLogger(__name__) class MsgId: - reference_clock = 0 last_time = 0 - msg_id_offset = 0 - server_time = 0 + offset = 0 def __new__(cls) -> int: - now = perf_counter() - cls.reference_clock + cls.server_time - cls.msg_id_offset = cls.msg_id_offset + 4 if now == cls.last_time else 0 - msg_id = int(now * 2 ** 32) + cls.msg_id_offset + now = time.time() + cls.offset = (cls.offset + 4) if now == cls.last_time else 0 + msg_id = int(now * 2 ** 32) + cls.offset cls.last_time = now return msg_id - - @classmethod - def set_server_time(cls, server_time: int): - if not cls.server_time: - cls.reference_clock = perf_counter() - cls.server_time = server_time - log.info(f"Time synced: {datetime.utcfromtimestamp(server_time)} UTC") diff --git a/pyrogram/session/session.py b/pyrogram/session/session.py index 5e5d93fa..6c3dbc03 100644 --- a/pyrogram/session/session.py +++ b/pyrogram/session/session.py @@ -203,9 +203,6 @@ class Session: log.debug(data) for msg in messages: - if msg.seq_no == 0: - MsgId.set_server_time(msg.msg_id / (2 ** 32)) - if msg.seq_no % 2 != 0: if msg.msg_id in self.pending_acks: continue