Skip to content

Commit

Permalink
Fix logger creating many thread locks when reloading the integrations…
Browse files Browse the repository at this point in the history
… page (#93768)

* Fix logger creating many thread locks

We call getLogger for each integration to get the current
log level when loading the integrations page. This creates
a storm of threading locks

* fixes
  • Loading branch information
bdraco authored Jan 14, 2024
1 parent d8564eb commit e4a1535
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 9 additions & 0 deletions homeassistant/components/logger/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import contextlib
from dataclasses import asdict, dataclass
from enum import StrEnum
from functools import lru_cache
import logging
from typing import Any, cast

Expand Down Expand Up @@ -216,3 +217,11 @@ async def async_get_levels(self, hass: HomeAssistant) -> dict[str, int]:
)

return dict(combined_logs)


get_logger = lru_cache(maxsize=256)(logging.getLogger)
"""Get a logger.
getLogger uses a threading.RLock, so we cache the result to avoid
locking the threads every time the integrations page is loaded.
"""
4 changes: 2 additions & 2 deletions homeassistant/components/logger/websocket_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Websocket API handlers for the logger integration."""
import logging
from typing import Any

import voluptuous as vol
Expand All @@ -16,6 +15,7 @@
LogPersistance,
LogSettingsType,
async_get_domain_config,
get_logger,
)


Expand All @@ -38,7 +38,7 @@ def handle_integration_log_info(
[
{
"domain": integration,
"level": logging.getLogger(
"level": get_logger(
f"homeassistant.components.{integration}"
).getEffectiveLevel(),
}
Expand Down

0 comments on commit e4a1535

Please sign in to comment.