Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions homeassistant/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ def async_enable_logging(
logging.getLogger("urllib3").setLevel(logging.WARNING)
logging.getLogger("aiohttp.access").setLevel(logging.WARNING)

sys.excepthook = lambda *args: logging.getLogger(None).exception(
"Uncaught exception", exc_info=args # type: ignore
)

# Log errors to a file if we have write access to file or config dir
if log_file is None:
err_log_path = hass.config.path(ERROR_LOG_FILENAME)
Expand Down
2 changes: 2 additions & 0 deletions homeassistant/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
from homeassistant.util import location
from homeassistant.util.async_ import fire_coroutine_threadsafe, run_callback_threadsafe
import homeassistant.util.dt as dt_util
from homeassistant.util.thread import fix_threading_exception_logging
from homeassistant.util.unit_system import IMPERIAL_SYSTEM, METRIC_SYSTEM, UnitSystem

# Typing imports that create a circular dependency
Expand All @@ -80,6 +81,7 @@


block_async_io.enable()
fix_threading_exception_logging()

# pylint: disable=invalid-name
T = TypeVar("T")
Expand Down
26 changes: 26 additions & 0 deletions homeassistant/util/thread.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Threading util helpers."""
import sys
import threading
from typing import Any


def fix_threading_exception_logging() -> None:
"""Fix threads passing uncaught exceptions to our exception hook.

https://bugs.python.org/issue1230540
Fixed in Python 3.8.
"""
if sys.version_info[:2] >= (3, 8):
return

run_old = threading.Thread.run

def run(*args: Any, **kwargs: Any) -> None:
try:
run_old(*args, **kwargs)
except (KeyboardInterrupt, SystemExit): # pylint: disable=try-except-raise
raise
except Exception: # pylint: disable=broad-except
sys.excepthook(*sys.exc_info())

threading.Thread.run = run # type: ignore