From eddc1ab778a31f25500f3f1035288cc6d565fd61 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 20 May 2021 18:06:37 -0500 Subject: [PATCH] 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 --- homeassistant/util/executor.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/homeassistant/util/executor.py b/homeassistant/util/executor.py index 6765fc5d8ae..c25c6b9c13f 100644 --- a/homeassistant/util/executor.py +++ b/homeassistant/util/executor.py @@ -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