Skip to content

Commit 271a4ba

Browse files
authored
Fix Core deadlock by ensuring only one ZHA log queue handler thread is running at a time (#142568)
Ensure only one log queue handler is running at a time
1 parent 528ca49 commit 271a4ba

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

homeassistant/components/zha/helpers.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ def __init__(
514514
self._log_queue_handler.listener = logging.handlers.QueueListener(
515515
log_simple_queue, log_relay_handler
516516
)
517+
self._log_queue_handler_count: int = 0
517518

518519
self._unsubs: list[Callable[[], None]] = []
519520
self._unsubs.append(self.gateway.on_all_events(self._handle_event_protocol))
@@ -747,7 +748,10 @@ def async_enable_debug_mode(self, filterer: _LogFilterType | None = None) -> Non
747748
if filterer:
748749
self._log_queue_handler.addFilter(filterer)
749750

750-
if self._log_queue_handler.listener:
751+
# Only start a new log queue handler if the old one is no longer running
752+
self._log_queue_handler_count += 1
753+
754+
if self._log_queue_handler.listener and self._log_queue_handler_count == 1:
751755
self._log_queue_handler.listener.start()
752756

753757
for logger_name in DEBUG_RELAY_LOGGERS:
@@ -763,7 +767,10 @@ def async_disable_debug_mode(self, filterer: _LogFilterType | None = None) -> No
763767
for logger_name in DEBUG_RELAY_LOGGERS:
764768
logging.getLogger(logger_name).removeHandler(self._log_queue_handler)
765769

766-
if self._log_queue_handler.listener:
770+
# Only stop the log queue handler if nothing else is using it
771+
self._log_queue_handler_count -= 1
772+
773+
if self._log_queue_handler.listener and self._log_queue_handler_count == 0:
767774
self._log_queue_handler.listener.stop()
768775

769776
if filterer:

0 commit comments

Comments
 (0)