fix: prevent shutdown deadlock and unblockable Ctrl+C on exit

Move stream close outside the lock in shutdown() to prevent deadlock
when audio callback tries to acquire the same lock. Replace single
t.join(timeout) with a polling loop (0.1s intervals) so KeyboardInterrupt
is not blocked during stream cleanup.
This commit is contained in:
0xbyt4 2026-03-13 23:55:12 +03:00
parent 69cb373864
commit 41162e0aca

View file

@ -400,7 +400,10 @@ class AudioRecorder:
t = threading.Thread(target=_do_close, daemon=True)
t.start()
t.join(timeout=timeout)
# Poll in short intervals so Ctrl+C is not blocked
deadline = __import__("time").monotonic() + timeout
while t.is_alive() and __import__("time").monotonic() < deadline:
t.join(timeout=0.1)
if t.is_alive():
logger.warning("Audio stream close timed out after %.1fs — forcing ahead", timeout)
@ -465,6 +468,7 @@ class AudioRecorder:
self._recording = False
self._frames = []
self._on_silence_stop = None
# Close stream OUTSIDE the lock to avoid deadlock with audio callback
self._close_stream_with_timeout()
logger.info("AudioRecorder shut down")