2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 13:27:47 +00:00

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
This commit is contained in:
Dan 2019-08-10 22:37:07 +02:00
parent 5665f980ea
commit c1321a4c01

View File

@ -45,7 +45,7 @@ from pyrogram.errors import (
PhoneCodeExpired, PhoneCodeEmpty, SessionPasswordNeeded, PhoneCodeExpired, PhoneCodeEmpty, SessionPasswordNeeded,
PasswordHashInvalid, FloodWait, PeerIdInvalid, FirstnameInvalid, PhoneNumberBanned, PasswordHashInvalid, FloodWait, PeerIdInvalid, FirstnameInvalid, PhoneNumberBanned,
VolumeLocNotFound, UserMigrate, ChannelPrivate, PhoneNumberOccupied, VolumeLocNotFound, UserMigrate, ChannelPrivate, PhoneNumberOccupied,
PasswordRecoveryNa, PasswordEmpty PasswordRecoveryNa, PasswordEmpty, AuthBytesInvalid
) )
from pyrogram.session import Auth, Session from pyrogram.session import Auth, Session
from .ext import utils, Syncer, BaseClient, Dispatcher from .ext import utils, Syncer, BaseClient, Dispatcher
@ -1720,27 +1720,32 @@ class Client(Methods, BaseClient):
if session is None: if session is None:
if dc_id != self.storage.dc_id: if dc_id != self.storage.dc_id:
session = Session(self, dc_id, Auth(self, dc_id).create(), is_media=True)
session.start()
for _ in range(3):
exported_auth = self.send( exported_auth = self.send(
functions.auth.ExportAuthorization( functions.auth.ExportAuthorization(
dc_id=dc_id dc_id=dc_id
) )
) )
session = Session(self, dc_id, Auth(self, dc_id).create(), is_media=True) try:
session.start()
self.media_sessions[dc_id] = session
session.send( session.send(
functions.auth.ImportAuthorization( functions.auth.ImportAuthorization(
id=exported_auth.id, id=exported_auth.id,
bytes=exported_auth.bytes bytes=exported_auth.bytes
) )
) )
except AuthBytesInvalid:
continue
else:
break
else:
session.stop()
raise AuthBytesInvalid
else: else:
session = Session(self, dc_id, self.storage.auth_key, is_media=True) session = Session(self, dc_id, self.storage.auth_key, is_media=True)
session.start() session.start()
self.media_sessions[dc_id] = session self.media_sessions[dc_id] = session