Skip to content

proposed fix for #2542: proper treatment of pooled_function exception… #2543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions telegram/ext/utils/promise.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,24 @@ def __setattr__(self, key: str, value: object) -> None:

def run(self) -> None:
"""Calls the :attr:`pooled_function` callable."""

try:
self._result = self.pooled_function(*self.args, **self.kwargs)

except Exception as exc:
self._exception = exc

finally:
self.done.set()
if self._done_callback:
try:
self._done_callback(self.result())
self._done_callback(self._result, self._exception)
except Exception as exc:
logger.warning(
"`done_callback` of a Promise raised the following exception."
" The exception won't be handled by error handlers."
)
logger.warning("Full traceback:", exc_info=exc)


def __call__(self) -> None:
self.run()

Expand All @@ -134,21 +134,23 @@ def result(self, timeout: float = None) -> Optional[RT]:

def add_done_callback(self, callback: Callable) -> None:
"""
Callback to be run when :class:`telegram.ext.utils.promise.Promise` becomes done.
Callback to be run when :class:`telegram.ext.utils.promise.Promise` becomes done
or throws 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.
callback will be called by passing ``Promise.result()`` and an instance of ``Exception``
occurred if any

"""
if self.done.wait(0):
callback(self.result())
callback(self._result, self._exception)
else:
self._done_callback = callback

@property
def exception(self) -> Optional[Exception]:
"""The exception raised by :attr:`pooled_function` or ``None`` if no exception has been
raised (yet).
"""
raised (yet)."""
return self._exception