diff --git a/telegram/ext/utils/promise.py b/telegram/ext/utils/promise.py index ff74bd56f82..6b548242972 100644 --- a/telegram/ext/utils/promise.py +++ b/telegram/ext/utils/promise.py @@ -100,7 +100,7 @@ def run(self) -> None: finally: self.done.set() - if self._done_callback: + if self._exception is None and self._done_callback: try: self._done_callback(self.result()) except Exception as exc: @@ -136,6 +136,10 @@ def add_done_callback(self, callback: Callable) -> None: """ Callback to be run when :class:`telegram.ext.utils.promise.Promise` becomes done. + Note: + Callback won't be called if :attr:`pooled_function` + raises an exception. + Args: callback (:obj:`callable`): The callable that will be called when promise is done. callback will be called by passing ``Promise.result()`` as only positional argument. diff --git a/tests/test_promise.py b/tests/test_promise.py index ace0a358d65..ceb9f196e7d 100644 --- a/tests/test_promise.py +++ b/tests/test_promise.py @@ -136,3 +136,17 @@ def done_callback(_): ) assert caplog.records[1].message.startswith("Full traceback:") assert promise.result() == "done!" + + def test_done_cb_not_run_on_excp(self): + def callback(): + raise TelegramError('Error') + + def done_callback(_): + self.test_flag = True + + promise = Promise(callback, [], {}) + promise.add_done_callback(done_callback) + promise.run() + assert isinstance(promise.exception, TelegramError) + assert promise.done + assert self.test_flag is False