From 4b3315db6feebafb94edcaa803df52bb49999ced Mon Sep 17 00:00:00 2001 From: Noam Meltzer Date: Sat, 21 Oct 2017 14:40:24 +0300 Subject: [PATCH] Fix race condition in dispatcher start/stop (#887) fixes #881 --- telegram/ext/dispatcher.py | 11 ++++++++++- telegram/ext/updater.py | 5 ++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/telegram/ext/dispatcher.py b/telegram/ext/dispatcher.py index 6bcf6b6e7..c229bcbd6 100644 --- a/telegram/ext/dispatcher.py +++ b/telegram/ext/dispatcher.py @@ -183,14 +183,20 @@ class Dispatcher(object): self.__async_queue.put(promise) return promise - def start(self): + def start(self, ready=None): """Thread target of thread 'dispatcher'. Runs in background and processes the update queue. + Args: + ready (:obj:`threading.Event`, optional): If specified, the event will be set once the + dispatcher is ready. + """ if self.running: self.logger.warning('already running') + if ready is not None: + ready.set() return if self.__exception_event.is_set(): @@ -202,6 +208,9 @@ class Dispatcher(object): self.running = True self.logger.debug('Dispatcher started') + if ready is not None: + ready.set() + while 1: try: # Pop update from update queue. diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index f5b07627e..e18284415 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -199,10 +199,13 @@ class Updater(object): # Create & start threads self.job_queue.start() - self._init_thread(self.dispatcher.start, "dispatcher") + dispatcher_ready = Event() + self._init_thread(self.dispatcher.start, "dispatcher", ready=dispatcher_ready) self._init_thread(self._start_polling, "updater", poll_interval, timeout, read_latency, bootstrap_retries, clean, allowed_updates) + dispatcher_ready.wait() + # Return the update queue so the main thread can insert updates return self.update_queue