From 9698862ba465ea03107e7f74ff7806038d64ebfc Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Wed, 14 Sep 2022 18:22:20 -0700 Subject: [PATCH 1/3] When chaining futures, skip callback if loop closed --- Lib/asyncio/futures.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/asyncio/futures.py b/Lib/asyncio/futures.py index 4bd9629e9eb62b..39776e3c2cce48 100644 --- a/Lib/asyncio/futures.py +++ b/Lib/asyncio/futures.py @@ -404,6 +404,8 @@ def _call_set_state(source): if dest_loop is None or dest_loop is source_loop: _set_state(destination, source) else: + if dest_loop.is_closed(): + return dest_loop.call_soon_threadsafe(_set_state, destination, source) destination.add_done_callback(_call_check_cancel) From 471792d2f63734207f105bf6bc9f4d594f662b5e Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Wed, 14 Sep 2022 18:35:02 -0700 Subject: [PATCH 2/3] When shutting down an executor, don't wake a closed loop --- Lib/asyncio/base_events.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py index a675fff1688d27..c6cbfb5139d038 100644 --- a/Lib/asyncio/base_events.py +++ b/Lib/asyncio/base_events.py @@ -577,9 +577,11 @@ async def shutdown_default_executor(self): def _do_shutdown(self, future): try: self._default_executor.shutdown(wait=True) - self.call_soon_threadsafe(future.set_result, None) + if not self.is_closed(): + self.call_soon_threadsafe(future.set_result, None) except Exception as ex: - self.call_soon_threadsafe(future.set_exception, ex) + if not self.is_closed(): + self.call_soon_threadsafe(future.set_exception, ex) def _check_running(self): if self.is_running(): From a396ba6d132a2241609b64ff5fd2bdef5ac10816 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 30 Sep 2022 15:56:23 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2022-09-30-15-56-20.gh-issue-96827.lzy1iw.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-09-30-15-56-20.gh-issue-96827.lzy1iw.rst diff --git a/Misc/NEWS.d/next/Library/2022-09-30-15-56-20.gh-issue-96827.lzy1iw.rst b/Misc/NEWS.d/next/Library/2022-09-30-15-56-20.gh-issue-96827.lzy1iw.rst new file mode 100644 index 00000000000000..159ab32ffbfc34 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-09-30-15-56-20.gh-issue-96827.lzy1iw.rst @@ -0,0 +1 @@ +Avoid spurious tracebacks from :mod:`asyncio` when default executor cleanup is delayed until after the event loop is closed (e.g. as the result of a keyboard interrupt).