From f06b9b68d510affb151c3137d0d44f2f172f42a4 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Mon, 7 May 2018 17:01:04 +0200 Subject: [PATCH] Fix race condition causing broken responses --- telethon/network/mtproto_sender.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/telethon/network/mtproto_sender.py b/telethon/network/mtproto_sender.py index 9b0be18a..c3209de9 100644 --- a/telethon/network/mtproto_sender.py +++ b/telethon/network/mtproto_sender.py @@ -57,6 +57,12 @@ class MtProtoSender: # Multithreading self._send_lock = Lock() + # If we're invoking something from an update thread but we're also + # receiving other request from the main thread (e.g. an update arrives + # and we need to process it) we must ensure that only one is calling + # receive at a given moment, since the receive step is fragile. + self._recv_lock = Lock() + def connect(self): """Connects to the server.""" self.connection.connect(self.session.server_address, self.session.port) @@ -132,8 +138,12 @@ class MtProtoSender: the UpdateState that will process all the received Update and Updates objects. """ + if self._recv_lock.locked(): + return + try: - body = self.connection.recv() + with self._recv_lock: + body = self.connection.recv() except (BufferError, InvalidChecksumError): # TODO BufferError, we should spot the cause... # "No more bytes left"; something wrong happened, clear