Fix race condition causing broken responses

This commit is contained in:
Lonami Exo 2018-05-07 17:01:04 +02:00
parent 2922e8df11
commit f06b9b68d5

View File

@ -57,6 +57,12 @@ class MtProtoSender:
# Multithreading # Multithreading
self._send_lock = Lock() 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): def connect(self):
"""Connects to the server.""" """Connects to the server."""
self.connection.connect(self.session.server_address, self.session.port) self.connection.connect(self.session.server_address, self.session.port)
@ -132,8 +138,12 @@ class MtProtoSender:
the UpdateState that will process all the received the UpdateState that will process all the received
Update and Updates objects. Update and Updates objects.
""" """
if self._recv_lock.locked():
return
try: try:
body = self.connection.recv() with self._recv_lock:
body = self.connection.recv()
except (BufferError, InvalidChecksumError): except (BufferError, InvalidChecksumError):
# TODO BufferError, we should spot the cause... # TODO BufferError, we should spot the cause...
# "No more bytes left"; something wrong happened, clear # "No more bytes left"; something wrong happened, clear