fix(audio): clear audioSwitch synchronously in AudioSwitchHandler.stop()#967
Merged
davidliu merged 1 commit intoJun 17, 2026
Merged
Conversation
Audio output could get stuck on the earpiece (very low volume) after a disconnect() + connect() on a reused Room on Android 12+ (CommDeviceAudioSwitch). stop() nulled `audioSwitch` inside the posted teardown runnable while tearing down handler/thread synchronously. Because the field was not volatile and the write happened off the lock on the handler thread, a fast subsequent start() could read a stale (already-stopped) switch and skip re-creation via the `if (audioSwitch == null)` guard. With no new switch created, activate() never re-ran, so the routing cleared by the prior deactivate()'s clearCommunicationDevice() was never re-asserted and playout stayed on the earpiece until the next connect. Clear `audioSwitch` synchronously under the lock and mark it @volatile so start() reliably observes the teardown and re-creates the switch. The switch's stop() is still posted to its handler thread, since AbstractAudioSwitch is not threadsafe. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: d0cee0d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
davidliu
approved these changes
Jun 17, 2026
Contributor
|
Thanks for the PR! |
Open
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.
Summary
On Android 12+ (the
CommDeviceAudioSwitchpath), audio output can get stuck on the earpiece at very low volume after adisconnect()+connect()on a reusedRoominstance (e.g. switching between rooms without releasing/recreating theRoom). The first connection routes to the speaker correctly; a subsequent reconnect intermittently does not.Root cause
A race in
AudioSwitchHandlerbetweenstop()and the nextstart():stop()nullsaudioSwitchinside the posted teardown runnable (on the handler thread) while tearing downhandler/threadsynchronously.audioSwitchis a non-@Volatilefield, and that write happens off the lock on the handler thread.start()(a different thread, under the@Synchronizedlock) can therefore read a stale, already-stopped switch and skip re-creation via theif (audioSwitch == null)guard.With no new switch created,
activate()never re-runs, so the route that the priordeactivate()cleared viaclearCommunicationDevice()(reverting the OS to the earpiece) is never re-asserted.Instrumented logs from a reproduction (good connect, then a bad reconnect on the same
Room):The
audioSwitch = nullwrite is wall-clock before thestart()read, yetstart()observes non-null — a memory-visibility issue, compounded by the structural asymmetry thathandler/threadare nulled synchronously whileaudioSwitchis not.Fix
Clear
audioSwitchsynchronously under the lock instop()(capturing the instance into a local so itsstop()can still be posted to the handler thread, sinceAbstractAudioSwitchis not threadsafe), and mark the field@Volatile. This guarantees the nextstart()observes the teardown and re-creates the switch.Verification
With the fix, every reconnect on the reused
Roomlogsstart(): audioSwitch == null -> creating a new switch→activate()→selected=Speakerphone, and audio reliably routes to the speaker (no moreSKIPPING re-create). Verified on-device (Android 15, Moto G45 5G) across repeated room switches.🤖 Generated with Claude Code