GH-96827: Don't touch closed loops from executor threads (GH-96837)
https://github.com/python/cpython/commit/a5c503f296a104d8b5b2e917a2f1ef71be8... commit: a5c503f296a104d8b5b2e917a2f1ef71be888e1b branch: 3.10 author: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> committer: miss-islington <31488909+miss-islington@users.noreply.github.com> date: 2022-09-30T13:25:06-07:00 summary: GH-96827: Don't touch closed loops from executor threads (GH-96837) * When chaining futures, skip callback if loop closed. * When shutting down an executor, don't wake a closed loop. (cherry picked from commit e9d63760fea8748638f6e495b5b07bd1805c9591) Co-authored-by: Guido van Rossum <guido@python.org> files: A Misc/NEWS.d/next/Library/2022-09-30-15-56-20.gh-issue-96827.lzy1iw.rst M Lib/asyncio/base_events.py M Lib/asyncio/futures.py diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py index ea10399b9413..1cc26ee7ccf0 100644 --- a/Lib/asyncio/base_events.py +++ b/Lib/asyncio/base_events.py @@ -573,9 +573,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(): diff --git a/Lib/asyncio/futures.py b/Lib/asyncio/futures.py index 48a32f868385..5d00ab4c8d31 100644 --- a/Lib/asyncio/futures.py +++ b/Lib/asyncio/futures.py @@ -396,6 +396,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) 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 000000000000..159ab32ffbfc --- /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).
participants (1)
-
miss-islington