Skip to content

Commit 9560952

Browse files
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 <[email protected]>
1 parent 3124618 commit 9560952

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

Lib/asyncio/base_events.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -577,9 +577,11 @@ async def shutdown_default_executor(self):
577577
def _do_shutdown(self, future):
578578
try:
579579
self._default_executor.shutdown(wait=True)
580-
self.call_soon_threadsafe(future.set_result, None)
580+
if not self.is_closed():
581+
self.call_soon_threadsafe(future.set_result, None)
581582
except Exception as ex:
582-
self.call_soon_threadsafe(future.set_exception, ex)
583+
if not self.is_closed():
584+
self.call_soon_threadsafe(future.set_exception, ex)
583585

584586
def _check_running(self):
585587
if self.is_running():

Lib/asyncio/futures.py

+2
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,8 @@ def _call_set_state(source):
404404
if dest_loop is None or dest_loop is source_loop:
405405
_set_state(destination, source)
406406
else:
407+
if dest_loop.is_closed():
408+
return
407409
dest_loop.call_soon_threadsafe(_set_state, destination, source)
408410

409411
destination.add_done_callback(_call_check_cancel)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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).

0 commit comments

Comments
 (0)