Skip to content

[Fix]AudioEngine ADM teardown after factory dealloc#78

Merged
ipavlidakis merged 1 commit intodevelopfrom
iliaspavlidakis/ios-1483-bugwebrtc-crash-on-audioengine-after-call-left
Mar 13, 2026
Merged

[Fix]AudioEngine ADM teardown after factory dealloc#78
ipavlidakis merged 1 commit intodevelopfrom
iliaspavlidakis/ios-1483-bugwebrtc-crash-on-audioengine-after-call-left

Conversation

@ipavlidakis
Copy link
Copy Markdown
Contributor

@ipavlidakis ipavlidakis commented Mar 13, 2026

Issues

Resolves https://linear.app/stream/issue/IOS-1483/bugwebrtc-crash-on-audioengine-after-call-left

Summary

This fixes a crash in the AudioEngine-based ADM when RTCAudioDeviceModule outlives its RTCPeerConnectionFactory.

The crash happens on audio route changes (AVAudioSessionRouteChangeNotification) after the factory has already torn down its worker thread. In that state, AudioEngineDevice can still be registered as an RTCAudioSession delegate and attempts to post back to a dead webrtc::Thread*, causing the EXC_BAD_ACCESS seen in crash reports.

Root cause

RTCPeerConnectionFactory owns the worker thread, but callers can retain factory.audioDeviceModule independently.

Before this change:

  • the factory could be deallocated
  • the worker thread could be destroyed
  • the retained ObjC ADM wrapper could still keep the native AudioEngineDevice alive
  • AudioEngineDevice remained registered with RTCAudioSession
  • the next route change called AudioEngineDevice::OnValidRouteChange()
  • that method called thread_->PostTask(...) on a stale raw thread pointer

Repro

A consistent repro sequence is:

  1. Create a RTCPeerConnectionFactory with RTCAudioDeviceModuleTypeAudioEngine.
  2. Keep a strong reference to factory.audioDeviceModule.
  3. Release the factory.
  4. Trigger an audio route change:
    • plug/unplug wired headphones
    • connect/disconnect Bluetooth audio
    • switch speaker/receiver output
  5. Observe a crash from audioSessionDidChangeRoute -> OnValidRouteChange -> PostTask.

Changes

  • Release the factory's native ADM reference before invalidating the ObjC ADM wrapper.
  • During wrapper invalidation, drop the wrapper's native ADM reference on the worker thread so native teardown happens while the stored thread is still valid.
  • Remove the stale RTCAudioSession delegate as part of that native teardown path.
  • Harden RTCAudioDeviceModule so that, once detached from the factory, late calls fail closed instead of touching _workerThread or _native.
    • arrays return empty
    • object accessors return nil
    • booleans return NO
    • mutating calls return -1 / NO
    • void methods become no-ops

Tests

Added an iOS regression test that:

  • creates an AudioEngine-backed factory
  • retains factory.audioDeviceModule
  • deallocates the factory
  • verifies the RTCAudioSession delegate count returns to baseline
  • verifies late calls on the retained wrapper are safe
  • injects a route-change notification to confirm the stale delegate path is gone

Verification

  • Generated an iOS simulator build with:
    • gn gen out/ios_sim_audioengine --args='target_os="ios" target_cpu="arm64" target_environment="simulator" enable_run_ios_unittests_with_xctest=true ios_enable_code_signing=false'
  • Ran direct clang compile-command checks for:
    • sdk/objc/api/peerconnection/RTCPeerConnectionFactory.mm
    • sdk/objc/api/peerconnection/RTCAudioDeviceModule.mm
    • sdk/objc/unittests/RTCPeerConnectionFactoryAudioEngine_xctest.mm

@ipavlidakis ipavlidakis self-assigned this Mar 13, 2026
@ipavlidakis ipavlidakis added the bug Something isn't working label Mar 13, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 13, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 583d07bd-5056-480c-90b6-d977fc3119ba

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • ✅ Review completed - (🔄 Check again to review again)
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch iliaspavlidakis/ios-1483-bugwebrtc-crash-on-audioengine-after-call-left
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@ipavlidakis ipavlidakis merged commit 30ce945 into develop Mar 13, 2026
1 check passed
@ipavlidakis ipavlidakis deleted the iliaspavlidakis/ios-1483-bugwebrtc-crash-on-audioengine-after-call-left branch March 13, 2026 20:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants