Skip to content

Commit c1321a4

Browse files
committed
Add smarter auth import to deal with race conditions by multi sessions
- Add a retry mechanism (up to three times) - Narrow the window in which export+import executes - Remove a line of duplicated code Fixes #299
1 parent 5665f98 commit c1321a4

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

pyrogram/client/client.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
PhoneCodeExpired, PhoneCodeEmpty, SessionPasswordNeeded,
4646
PasswordHashInvalid, FloodWait, PeerIdInvalid, FirstnameInvalid, PhoneNumberBanned,
4747
VolumeLocNotFound, UserMigrate, ChannelPrivate, PhoneNumberOccupied,
48-
PasswordRecoveryNa, PasswordEmpty
48+
PasswordRecoveryNa, PasswordEmpty, AuthBytesInvalid
4949
)
5050
from pyrogram.session import Auth, Session
5151
from .ext import utils, Syncer, BaseClient, Dispatcher
@@ -1229,7 +1229,7 @@ def send(self, data: TLObject, retries: int = Session.MAX_RETRIES, timeout: floa
12291229
def load_config(self):
12301230
parser = ConfigParser()
12311231
parser.read(str(self.config_file))
1232-
1232+
12331233
if self.bot_token:
12341234
pass
12351235
else:
@@ -1720,30 +1720,35 @@ def get_file(
17201720

17211721
if session is None:
17221722
if dc_id != self.storage.dc_id:
1723-
exported_auth = self.send(
1724-
functions.auth.ExportAuthorization(
1725-
dc_id=dc_id
1726-
)
1727-
)
1728-
17291723
session = Session(self, dc_id, Auth(self, dc_id).create(), is_media=True)
1730-
17311724
session.start()
17321725

1733-
self.media_sessions[dc_id] = session
1734-
1735-
session.send(
1736-
functions.auth.ImportAuthorization(
1737-
id=exported_auth.id,
1738-
bytes=exported_auth.bytes
1726+
for _ in range(3):
1727+
exported_auth = self.send(
1728+
functions.auth.ExportAuthorization(
1729+
dc_id=dc_id
1730+
)
17391731
)
1740-
)
1732+
1733+
try:
1734+
session.send(
1735+
functions.auth.ImportAuthorization(
1736+
id=exported_auth.id,
1737+
bytes=exported_auth.bytes
1738+
)
1739+
)
1740+
except AuthBytesInvalid:
1741+
continue
1742+
else:
1743+
break
1744+
else:
1745+
session.stop()
1746+
raise AuthBytesInvalid
17411747
else:
17421748
session = Session(self, dc_id, self.storage.auth_key, is_media=True)
1743-
17441749
session.start()
17451750

1746-
self.media_sessions[dc_id] = session
1751+
self.media_sessions[dc_id] = session
17471752

17481753
if media_type == 1:
17491754
location = types.InputPeerPhotoFileLocation(

0 commit comments

Comments
 (0)