From 69efad58496fab9c6aef337cf2b7706a0a2e1f91 Mon Sep 17 00:00:00 2001 From: Tor Arvid Lund Date: Thu, 9 Feb 2023 10:30:45 +0100 Subject: [PATCH 1/3] Handle exceptions in created tasks When create_task is called, the done_callback should check whether or not an exception was raised, and if so, re-raise it. --- aiormq/base.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/aiormq/base.py b/aiormq/base.py index b8bf9bd..9132747 100644 --- a/aiormq/base.py +++ b/aiormq/base.py @@ -35,6 +35,10 @@ def remover(*_: Any) -> None: if future in self.futures: self.futures.remove(future) + exc = future.exception() + if exc is not None: + raise exc + return remover def add(self, future: Union[asyncio.Future, TaskWrapper]) -> None: From fffa397a905f1d30442f6e2ad719f6bb585d8176 Mon Sep 17 00:00:00 2001 From: Tor Arvid Lund Date: Mon, 20 Feb 2023 13:10:03 +0100 Subject: [PATCH 2/3] Don't raise exception in task_done when cancelled --- aiormq/base.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/aiormq/base.py b/aiormq/base.py index 9132747..308bca0 100644 --- a/aiormq/base.py +++ b/aiormq/base.py @@ -35,9 +35,12 @@ def remover(*_: Any) -> None: if future in self.futures: self.futures.remove(future) - exc = future.exception() - if exc is not None: - raise exc + try: + exc = future.exception() + if exc is not None: + raise exc + except asyncio.CancelledError: + pass return remover From 566fddd20268223db3da0e10637a1f68375ec4de Mon Sep 17 00:00:00 2001 From: Mosquito Date: Mon, 20 Feb 2023 17:42:52 +0300 Subject: [PATCH 3/3] Remove closure --- aiormq/base.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/aiormq/base.py b/aiormq/base.py index 308bca0..1de1b1d 100644 --- a/aiormq/base.py +++ b/aiormq/base.py @@ -30,23 +30,18 @@ def __init__(self, loop: asyncio.AbstractEventLoop): def __on_task_done( self, future: Union[asyncio.Future, TaskWrapper], ) -> Callable[..., Any]: - def remover(*_: Any) -> None: - nonlocal future - if future in self.futures: - self.futures.remove(future) + if future in self.futures: + self.futures.remove(future) - try: - exc = future.exception() - if exc is not None: - raise exc - except asyncio.CancelledError: - pass + if not isinstance(future, TaskWrapper): + return - return remover + with suppress(asyncio.CancelledError): + future.result() def add(self, future: Union[asyncio.Future, TaskWrapper]) -> None: self.futures.add(future) - future.add_done_callback(self.__on_task_done(future)) + future.add_done_callback(self.__on_task_done) if self.parent: self.parent.add(future)