Update audio_engine.py

Summary

Add thread-safe locking for wake word reload logic

Problem

Wake word reload logic accessed self.state.wake_words_changed flag without synchronization, creating a potential race condition when wake words are dynamically switched via Home Assistant.

Changes

Added self._wake_words_lock = threading.Lock() in __init__
Wrapped wake word reload block with with self._wake_words_lock: context manager
Ensures atomic read-modify-write of wake_words_changed flag
Impact

Risk: Very low - purely defensive improvement
Benefit: Eliminates race condition in wake word switching
Performance: Negligible overhead (lock only held during reload check)
This commit is contained in:
imonlinux
2026-01-10 13:23:33 -08:00
committed by GitHub
parent fed867936a
commit 1da9ac1fac
+5
View File
@@ -28,6 +28,9 @@ class AudioEngine:
self.block_size = block_size self.block_size = block_size
self._thread: Optional[threading.Thread] = None self._thread: Optional[threading.Thread] = None
# CRITICAL FIX: Add lock for thread-safe wake word reload
self._wake_words_lock = threading.Lock()
def start(self): def start(self):
"""Starts the audio processing thread.""" """Starts the audio processing thread."""
self._thread = threading.Thread( self._thread = threading.Thread(
@@ -113,6 +116,8 @@ class AudioEngine:
time.sleep(0.01) time.sleep(0.01)
continue continue
# CRITICAL FIX: Protect wake word reload with lock
with self._wake_words_lock:
# Update active wake words if changed # Update active wake words if changed
if (not wake_words) or (self.state.wake_words_changed): if (not wake_words) or (self.state.wake_words_changed):
self.state.wake_words_changed = False self.state.wake_words_changed = False