Repository navigation
Keep session requirements released when the session update fails - #1233
hiroshihorie wants to merge 1 commit into
Conversation
AudioSessionEngineObserver rolled its requirements back whenever configuring the audio session failed, including on the release and engine-teardown paths. A failed setActive(false) then left a requirement that nothing could remove: the session was never deactivated again and the next start skipped activation. Treat a change where every requirement stayed the same or shrank as a release, and keep it committed when configuration fails. The error is still thrown. Acquiring or raising a requirement still rolls back. Since LiveKitWebRTC 150.7871.08 the audio device module only logs a failed teardown hook, so the engine has stopped either way and the observer must not keep the engine's requirement. Fixes #1154 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
There was a problem hiding this comment.
🔍 Devin Review: 1 flag
Not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)
| let isFailing = StateSync(false) | ||
| let lastOldState = StateSync<Observer.State?>(nil) | ||
|
|
||
| override func configureAudioSession(oldState: Observer.State, newState _: Observer.State) throws { |
There was a problem hiding this comment.
suggestion (test): We can override configureIfNeeded instead of configureAudioSession, because configureIfNeeded returns before it reaches the override while TrackTests sets customConfigureAudioSessionFunc. If the suites run in parallel, the "failing" calls then succeed. The suite also builds the AudioManager.shared singleton.
| if !isRelease { | ||
| $0 = oldState | ||
| } | ||
| throw LiveKitError(.audioSession, message: "Failed to configure audio session") |
There was a problem hiding this comment.
suggestion (non-blocking): Pass internalError: error to this LiveKitError, so that a caller can tell isBusy (the session went inactive) from a real failure.
| if !isRelease { | ||
| $0 = oldState | ||
| } | ||
| throw LiveKitError(.audioSession, message: "Failed to configure audio session") |
There was a problem hiding this comment.
thought (non-blocking): A committed release throws out of _state.mutate, so onDidMutate does not see the change. We can return the error from the block and throw after mutate.
| @Test func failedEngineDisableStillRemovesTheRequirement() throws { | ||
| let observer = FailingObserver() | ||
| let engine = AVAudioEngine() | ||
| _ = observer.engineWillEnable(engine, isPlayoutEnabled: true, isRecordingEnabled: true) |
There was a problem hiding this comment.
nitpick (test): Add #expect(... == 0) to the setup calls at lines 176 and 199. Split the && expectations at lines 154 and 190 into two, so that a failure shows which side failed.
Fixes #1154. Supersedes #1155 (same fix, plus the engine-teardown cases below).
Problem
AudioSessionEngineObserver.updateRequirementsrolls the requirements back to the old state whenever configuring the audio session fails. That is right when a requirement is being acquired, and wrong when one is being released:SessionRequirementHandle.release()gives up its release closure before running it. If the closure throws, the requirement stays registered and nothing is left to remove it.deinitandSoundPlayerboth calltry? release(), so nothing retries either.engineDidDisablehas the same problem with the engine's own requirement. Since LiveKitWebRTC 150.7871.08 (iOS: engine observer rollback contract (m150) libwebrtc#312) the audio device module only logs a failed teardown hook, so the engine has stopped regardless of what the observer returns.In both cases the observer keeps a requirement that nothing owns:
setActive(true), because the observer thinks the session is already activeThis is easy to hit before iOS 26. Releasing the last requirement calls
setActive(false), and if any I/O is still running (the app's ownAVPlayer, for example) the call returnsAVAudioSessionErrorCodeIsBusy. The session does go inactive, as theAVAudioSession.hheader says, so the rollback leaves the bookkeeping out of sync with the real session.Fix
In
updateRequirements, a change where every requirement stayed the same or shrank counts as a release. A release stays committed when configuration fails, and the error is still thrown. Acquiring or raising a requirement still rolls back, so a failedacquireregisters nothing and a failedengineWillEnableleaves no state behind.This also covers
engineDidDisablebeing delivered as compensation after a rolled-back enable, since it carries the earlier, smaller state.I did not take the retry approach suggested in the issue (putting the closure back when it throws). No caller in the SDK retries, and keeping the release committed avoids the question of what happens when two concurrent releases both fail. The docs for
release()andacquire(requirement:)now say what a thrown error means.configureAudioSessionis now internal instead of@Sendable private, so tests can override it to fail on demand. No public API changes.Tests
New cases in
AudioSessionCategorySelectionTestsuse a subclass that fails configuration on demand:release()does nothing, and the next acquire activates the session againengineWillEnablerolls backengineDidDisableremoves the engine requirement and clearshasRecorded, and the next engine start activates the session againengineDidDisable(mic stops, playout continues) still drops the recording requirementAll 13 tests in the suite pass on an iOS simulator.
swiftformat --lintandswiftlint --strictare clean.Not verified on a device: the real
setActive(false)failure (isBusy) has not been reproduced on an iPhone.🤖 Generated with Claude Code