From fa055fc80a2fd2dfa4b6c8c54b8f88f79994a872 Mon Sep 17 00:00:00 2001 From: jonathan Date: Wed, 10 Jun 2026 12:29:21 +0200 Subject: [PATCH] perf: do not hold the audio buffer lock during disk writes _flush_buffer_to_file wrote (and for FLAC, encoded) every chunk while holding buffer_lock, blocking the capture callback for the duration of each disk flush. Swap the buffer out under the lock and write outside. Co-Authored-By: Claude Fable 5 --- isr.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/isr.py b/isr.py index cc1d903..22fc974 100644 --- a/isr.py +++ b/isr.py @@ -714,11 +714,12 @@ class SoundcardRecorder(BaseRecorder): if self.current_file is None: return + # Swap the buffer under the lock, write outside it — disk writes (and + # FLAC encoding) must not block the capture callback. with self.buffer_lock: - if self.audio_buffer: - for data in self.audio_buffer: - self.current_file.write(data) - self.audio_buffer.clear() + buffered, self.audio_buffer = self.audio_buffer, [] + for data in buffered: + self.current_file.write(data) def close_current_file(self): """Flush any buffered audio, then close the current recording file."""