Make confirm_received a flag, avoid race conditions, fix bg RPCError

There was a race condition between TelegramBareClient.invoke
receiving part and MtProtoSender._handle_rpc_result actually
reading the result after setting request.confirmed = True.

The .confirmed is now a threading.Event to avoid the sleep(0.1).

RPC errors have been moved inside the request so they're not
raised on a background thread anymore.
This commit is contained in:
Lonami Exo
2017-09-02 20:41:00 +02:00
parent b908296efa
commit 863d2e8368
3 changed files with 16 additions and 15 deletions

View File

@@ -1,4 +1,5 @@
from datetime import datetime, timedelta
from threading import Event
class TLObject:
@@ -10,7 +11,8 @@ class TLObject:
self.dirty = False
self.send_time = None
self.confirm_received = False
self.confirm_received = Event()
self.rpc_error = None
# These should be overrode
self.constructor_id = 0
@@ -23,11 +25,11 @@ class TLObject:
self.sent = True
def on_confirm(self):
self.confirm_received = True
self.confirm_received.set()
def need_resend(self):
return self.dirty or (
self.content_related and not self.confirm_received and
self.content_related and not self.confirm_received.is_set() and
datetime.now() - self.send_time > timedelta(seconds=3))
@staticmethod