Conversation
…tic retry Co-authored-by: benlachman <100571+benlachman@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Handle error when arecord is busy for iOS client
Fix "Device or resource busy" error when switching audio inputs with automatic retry
Oct 22, 2025
Owner
|
@copilot Can we also just terminate the orphaned |
Co-authored-by: benlachman <100571+benlachman@users.noreply.github.com>
Author
Added orphaned process cleanup in commit 5053649. The solution includes:
This ensures that any orphaned arecord processes holding the audio device are cleaned up before attempting to start a new one, preventing the "Device or resource busy" error even when processes haven't been properly terminated. |
Copilot
AI
changed the title
Fix "Device or resource busy" error when switching audio inputs with automatic retry
Fix "Device or resource busy" error when switching audio inputs with automatic retry and orphaned process cleanup
Oct 22, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When an iOS client switches audio input to a USB device (e.g.,
plughw:0,0), the server frequently reports an error:This occurs due to two issues:
arecordprocess is killed and a new one is spawned immediately. However, the OS hasn't fully released the audio device yet, causing the new process to fail with EBUSY.arecordprocesses don't terminate properly and remain running even after babelpod attempts to kill them, holding the audio device.Solution
This PR implements a three-layer approach to handle these issues:
1. Prevention: Cleanup Delay
Added a 500ms delay after killing the old
arecordprocess before starting the new one. This gives the OS sufficient time to release the audio device in most cases, significantly reducing the frequency of busy errors.2. Orphaned Process Cleanup
cleanupCurrentInput()to use SIGTERM first for graceful termination, then SIGKILL after 100ms if the process doesn't diekillOrphanedArecord()function that runs before starting a new arecord:pgrep -f "arecord.*${devId}"to find any existing processes for the devicepkill -9(SIGKILL) to ensure they die3. Recovery: Automatic Retry with Exponential Backoff
When a "Device or resource busy" error is detected:
Implementation Details
New function
killOrphanedArecord(devId):New helper function
startArecordForDevice(devId, isRetry):killOrphanedArecord()before spawning new processEnhanced
cleanupCurrentInput():Enhanced
setupArecordHandlers(devId, isRetry):Modified
switch_inputhandler:User Experience
Before: User encounters error and must manually retry, possibly multiple times. Orphaned processes can persist and block the device indefinitely.
After: System automatically handles retries transparently and cleans up orphaned processes. Most cases succeed within 700ms (cleanup delay + one retry). User only sees an error if the device is genuinely unavailable after 5 attempts (~6.7 seconds).
Testing
Added 10 comprehensive unit tests covering:
All 26 tests pass (16 existing + 10 new) with no regressions.
Edge Cases Handled
Performance Impact
Fixes #10
Original prompt
Fixes #10
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.