From 8339f493bc84abe3ca2ddf14ebbb919fcf4358f4 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 31 Dec 2017 11:46:42 +0100 Subject: [PATCH] Don't retry forever --- pyrogram/session/auth.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pyrogram/session/auth.py b/pyrogram/session/auth.py index 928ed9d8..90899cc5 100644 --- a/pyrogram/session/auth.py +++ b/pyrogram/session/auth.py @@ -32,6 +32,8 @@ log = logging.getLogger(__name__) class Auth: + MAX_RETRIES = 5 + CURRENT_DH_PRIME = int( "C71CAEB9C6B1C9048E6C522F70F13F73980D40238E3E21C14934D037563D930F" "48198A0AA7C14058229493D22530F4DBFA336F6E0AC925139543AED44CCE7C37" @@ -76,9 +78,10 @@ class Auth: https://core.telegram.org/mtproto/auth_key https://core.telegram.org/mtproto/samples-auth_key """ + retries_left = self.MAX_RETRIES # The server may close the connection at any time, causing the auth key creation to fail. - # If that happens, just try again until it succeed. + # If that happens, just try again up to MAX_RETRIES times. while True: try: log.info("Start creating a new auth key on DC{}".format(self.dc_id)) @@ -243,6 +246,11 @@ class Auth: ) ) except Exception as e: + if retries_left: + retries_left -= 1 + else: + raise e + log.warning("Auth key creation failed. Let's try again: {}".format(repr(e))) time.sleep(1) continue