Handle threads exiting unexpected during shutdown (#50907)

If a thread exits right when we are trying to force an exception
to shut it down, setting the exception will fail with SystemError.
At this point in the shutdown process we want to move on as this
will cause the shutdown to abort
This commit is contained in:
J. Nick Koston 2021-05-20 18:06:37 -05:00 committed by GitHub
parent 0623648309
commit eddc1ab778
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@
from __future__ import annotations
from concurrent.futures import ThreadPoolExecutor
import contextlib
import logging
import queue
import sys
@ -49,7 +50,11 @@ def join_or_interrupt_threads(
if log:
_log_thread_running_at_shutdown(thread.name, thread.ident)
async_raise(thread.ident, SystemExit)
with contextlib.suppress(SystemError):
# SystemError at this stage is usually a race condition
# where the thread happens to die right before we force
# it to raise the exception
async_raise(thread.ident, SystemExit)
return joined