Visitar URL original
[3.10] GH-96827: Don't touch closed loops from executor threads (GH-96837) by miss-islington · Pull Request #97691 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
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
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 e9d6376)

Co-authored-by: Guido van Rossum <guido@python.org>
  • Loading branch information
gvanrossum authored and miss-islington committed Sep 30, 2022
commit 0473fe74f839a17aa0039248374dcaef256d84c7
6 changes: 4 additions & 2 deletions Lib/asyncio/base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
2 changes: 2 additions & 0 deletions Lib/asyncio/futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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).