Skip to content

Keep session requirements released when the session update fails - #1233

Open
hiroshihorie wants to merge 1 commit into
mainfrom
hiroshi/clt-3679-session-bookkeeping-failed-teardown
Open

hiroshihorie wants to merge 1 commit into
mainfrom
hiroshi/clt-3679-session-bookkeeping-failed-teardown

Conversation

@hiroshihorie

Copy link
Copy Markdown
Member

Fixes #1154. Supersedes #1155 (same fix, plus the engine-teardown cases below).

Problem

AudioSessionEngineObserver.updateRequirements rolls 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. deinit and SoundPlayer both call try? release(), so nothing retries either.
  • engineDidDisable has 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:

  • the session is never deactivated again, not even after a call ends
  • the next start skips setActive(true), because the observer thinks the session is already active

This 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 own AVPlayer, for example) the call returns AVAudioSessionErrorCodeIsBusy. The session does go inactive, as the AVAudioSession.h header 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 failed acquire registers nothing and a failed engineWillEnable leaves no state behind.

This also covers engineDidDisable being 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() and acquire(requirement:) now say what a thrown error means.

configureAudioSession is now internal instead of @Sendable private, so tests can override it to fail on demand. No public API changes.

Tests

New cases in AudioSessionCategorySelectionTests use a subclass that fails configuration on demand:

  • a failed release removes the requirement, a second release() does nothing, and the next acquire activates the session again
  • a failed acquire registers nothing
  • a failed engineWillEnable rolls back
  • a failed engineDidDisable removes the engine requirement and clears hasRecorded, and the next engine start activates the session again
  • a failed partial engineDidDisable (mic stops, playout continues) still drops the recording requirement

All 13 tests in the suite pass on an iOS simulator. swiftformat --lint and swiftlint --strict are clean.

Not verified on a device: the real setActive(false) failure (isBusy) has not been reproduced on an iPhone.

🤖 Generated with Claude Code

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>
@hiroshihorie
hiroshihorie marked this pull request as ready for review November 24, 2026 05:34

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Devin Review: 1 flag

Not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

let isFailing = StateSync(false)
let lastOldState = StateSync<Observer.State?>(nil)

override func configureAudioSession(oldState: Observer.State, newState _: Observer.State) throws {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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.

SessionRequirementHandle.release() loses cleanup after a failure, making retries no-ops

2 participants