Skip to content

Commit e9d6376

Browse files
authored
GH-96827: Don't touch closed loops from executor threads (#96837)
* When chaining futures, skip callback if loop closed. * When shutting down an executor, don't wake a closed loop.
1 parent b05dd79 commit e9d6376

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

Lib/asyncio/base_events.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,9 +588,11 @@ async def shutdown_default_executor(self, timeout=None):
588588
def _do_shutdown(self, future):
589589
try:
590590
self._default_executor.shutdown(wait=True)
591-
self.call_soon_threadsafe(future.set_result, None)
591+
if not self.is_closed():
592+
self.call_soon_threadsafe(future.set_result, None)
592593
except Exception as ex:
593-
self.call_soon_threadsafe(future.set_exception, ex)
594+
if not self.is_closed():
595+
self.call_soon_threadsafe(future.set_exception, ex)
594596

595597
def _check_running(self):
596598
if self.is_running():

Lib/asyncio/futures.py

Lines changed: 2 additions & 0 deletions
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)
Lines changed: 1 addition & 0 deletions
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)