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.
This commit is contained in:
Guido van Rossum 2022-09-30 12:55:40 -07:00 committed by GitHub
parent b05dd79649
commit e9d63760fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 2 deletions

View file

@ -588,9 +588,11 @@ async def shutdown_default_executor(self, timeout=None):
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():

View file

@ -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)

View file

@ -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).