From 46cdeb495a0206c1200dc79baa120ff8bf65e6a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C9=91rry=20Shiv=C9=91m?= Date: Sat, 5 Jun 2021 20:38:45 +0530 Subject: [PATCH] Fix for Promise.done_callback (#2544) * Don't call done_cb on exceptions Signed-off-by: starry69 * improve docs Co-authored-by: Bibo-Joshi * revert black Co-authored-by: Bibo-Joshi --- telegram/ext/utils/promise.py | 6 +++++- tests/test_promise.py | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) 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