Skip to content

Commit 4b3315d

Browse files
authored
Fix race condition in dispatcher start/stop (python-telegram-bot#887)
fixes python-telegram-bot#881
1 parent 3ed0599 commit 4b3315d

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

telegram/ext/dispatcher.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,20 @@ def run_async(self, func, *args, **kwargs):
183183
self.__async_queue.put(promise)
184184
return promise
185185

186-
def start(self):
186+
def start(self, ready=None):
187187
"""Thread target of thread 'dispatcher'.
188188
189189
Runs in background and processes the update queue.
190190
191+
Args:
192+
ready (:obj:`threading.Event`, optional): If specified, the event will be set once the
193+
dispatcher is ready.
194+
191195
"""
192196
if self.running:
193197
self.logger.warning('already running')
198+
if ready is not None:
199+
ready.set()
194200
return
195201

196202
if self.__exception_event.is_set():
@@ -202,6 +208,9 @@ def start(self):
202208
self.running = True
203209
self.logger.debug('Dispatcher started')
204210

211+
if ready is not None:
212+
ready.set()
213+
205214
while 1:
206215
try:
207216
# Pop update from update queue.

telegram/ext/updater.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,13 @@ def start_polling(self,
199199

200200
# Create & start threads
201201
self.job_queue.start()
202-
self._init_thread(self.dispatcher.start, "dispatcher")
202+
dispatcher_ready = Event()
203+
self._init_thread(self.dispatcher.start, "dispatcher", ready=dispatcher_ready)
203204
self._init_thread(self._start_polling, "updater", poll_interval, timeout,
204205
read_latency, bootstrap_retries, clean, allowed_updates)
205206

207+
dispatcher_ready.wait()
208+
206209
# Return the update queue so the main thread can insert updates
207210
return self.update_queue
208211

0 commit comments

Comments
 (0)