diff --git a/telethon/telegram_bare_client.py b/telethon/telegram_bare_client.py index d173a1c1..75928b69 100644 --- a/telethon/telegram_bare_client.py +++ b/telethon/telegram_bare_client.py @@ -411,9 +411,6 @@ class TelegramBareClient: The invoke will be retried up to 'retries' times before raising ValueError(). """ - # Any error from a background thread will be "posted" and checked here - self.updates.check_error() - if not all(isinstance(x, TLObject) and x.content_related for x in requests): raise ValueError('You can only invoke requests, not types!') @@ -775,9 +772,16 @@ class TelegramBareClient: while self._user_connected and not self._reconnect(): sleep(0.1) # Retry forever, this is instant messaging - except Exception as e: + except Exception as error: # Unknown exception, pass it to the main thread - self.updates.set_error(e) + self._logger.debug( + '[ERROR] Unknown error on the read thread, please report', + error + ) + # If something strange happens we don't want to enter an + # infinite loop where all we do is raise an exception, so + # add a little sleep to avoid the CPU usage going mad. + sleep(0.1) break self._recv_thread = None diff --git a/telethon/update_state.py b/telethon/update_state.py index 4402fb14..7e25549f 100644 --- a/telethon/update_state.py +++ b/telethon/update_state.py @@ -55,7 +55,7 @@ class UpdateState: self._updates_available.clear() if isinstance(update, Exception): - raise update # Some error was set through .set_error() + raise update # Some error was set through (surely StopIteration) return update @@ -79,7 +79,12 @@ class UpdateState: """Raises "StopIterationException" on the worker threads to stop them, and also clears all of them off the list """ - self.set_error(StopIteration()) + with self._updates_lock: + # Insert at the beginning so the very next poll causes an error + # TODO Should this reset the pts and such? + self._updates.appendleft(StopIteration()) + self._updates_available.set() + for t in self._worker_threads: t.join() @@ -116,21 +121,6 @@ class UpdateState: '[ERROR] Unhandled exception on worker {}'.format(wid), e ) - def set_error(self, error): - """Sets an error, so that the next call to .poll() will raise it. - Can be (and is) used to pass exceptions between threads. - """ - with self._updates_lock: - # Insert at the beginning so the very next poll causes an error - # TODO Should this reset the pts and such? - self._updates.appendleft(error) - self._updates_available.set() - - def check_error(self): - with self._updates_lock: - if self._updates and isinstance(self._updates[0], Exception): - raise self._updates.popleft() - def process(self, update): """Processes an update object. This method is normally called by the library itself.