Skip to content

Improve Audio Recording Performance by Removing Blocking I/O from Callback Path#20

Open
nikhil1205-ai wants to merge 1 commit intoruxailab:mainfrom
nikhil1205-ai:fix/upgrade_Audio_Writing
Open

Improve Audio Recording Performance by Removing Blocking I/O from Callback Path#20
nikhil1205-ai wants to merge 1 commit intoruxailab:mainfrom
nikhil1205-ai:fix/upgrade_Audio_Writing

Conversation

@nikhil1205-ai
Copy link
Copy Markdown

This PR refactors the audio recording pipeline in AudioSink to eliminate blocking disk I/O from the real-time audio callback path.

The previous implementation performed synchronous writes (wave.writeframes) inside the write() method under a global lock, which could introduce latency and packet loss during multi-user voice activity.

Problem #19

The existing implementation:

with self._lock:
    writer.writeframes(data)

Issues:

  • Blocking disk I/O in real-time audio ingestion path
  • Global lock causes contention across multiple users
  • Poor scalability for concurrent speakers
  • Risk of dropped or delayed audio packets

Solution

This PR introduces a queue-based buffering system with a background worker thread:

Changes:

  • Audio data is now enqueued in write() instead of written directly
  • A dedicated worker thread processes the queue and performs disk writes
  • Lock usage is minimized to only protect shared state (_user_writers)
  • Disk I/O is performed outside the lock

Implementation Details

  1. Queue-based buffering
    self._queue = queue.Queue(maxsize=1000)

  2. Non-blocking write path
    self._queue.put((user_id, user, data), block=False)

  3. Background worker thread

def _worker(self):
    while True:
        item = self._queue.get()
        if item is None:
            break
        ...
        writer.writeframes(data)
  1. Graceful shutdown
self._queue.put(None)
self._thread.join()

Benefits

  • Eliminates blocking operations from audio callback path
  • Improves performance in multi-user voice scenarios
  • Reduces risk of audio packet loss
  • Scales better under concurrent load
  • Cleaner separation of concerns (ingestion vs I/O)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant