Repository navigation
mac-capture: CoreAudio reconnect backs off instead of retrying every 300 ms / 2 s forever (#1358) - #1370
Merged
Conversation
…fixed 300 ms / 2 s (#1358) The reconnect thread now waits per coreaudio-retry.h: start at retry_time, double to a 30 s cap (logged once at LOG_INFO), restart at the base when CoreAudio reports a device-list or default-device change. A listener on the system object signals a new auto wake_event the loop waits on; the exit_event is checked after every wake, and shutdown signals both. The success path and shutdown are otherwise unchanged. Refs #1358
zackees
force-pushed
the
fix/1358-coreaudio-reconnect-backoff
branch
from
October 18, 2026 00:39
95e22b6 to
3ba49f8
Compare
zackees
enabled auto-merge
October 18, 2026 00:39
zackees
disabled auto-merge
October 18, 2026 00:44
…not the source (#1358) The device-change listener callback now touches only a small heap context (atomic refcount, wake event, reset flag). The source holds one reference and each registered listener another, dropped after AudioObjectRemovePropertyListener returns; the last unref destroys the event and frees the context. A callback still running after the source is freed therefore never touches freed source state. test_coreaudio_retry gains cases for a late callback after the source's unref and for the single free. Refs #1358
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.
Refs #1358 (parent #1273). Not a closing reference: the real app was not run on a Mac here.
Cause
plugins/mac-capture/mac-audio.c.hreconnect thread:while (os_event_timedwait(exit_event, retry_time) == ETIMEDOUT) if (coreaudio_init(ca)) break;withretry_time300 ms (default-device change) or 2000 ms (unplug / failed first init), forever, no backoff. An unplugged device meant a full CoreAudio init attempt every 2 s (or 300 ms) for the rest of the session.Design
plugins/mac-capture/coreaudio-retry.h(no Apple headers):coreaudio_retry_reset(r, base)/coreaudio_retry_next(r, &cap_first). Waits start at the existingretry_time, double, cap at 30 s;cap_firstis true once per schedule.kAudioHardwarePropertyDevicesand the default-device property (the per-device listeners are removed while the device is down, so the existingnotification_callbackcannot fire then). The callback wakes the thread, which restarts the schedule at its base.struct coreaudio_wake(atomic refcount, auto wake event, reset flag) incoreaudio-retry.h, neverca. The callback only callscoreaudio_wake_signal(). The source holds one reference (created incoreaudio_create), and each successfully registered listener holds another, dropped only afterAudioObjectRemovePropertyListenerreturns. The lastcoreaudio_wake_unref()destroys the event and frees the context, socoreaudio_destroyno longer destroys the event itself. A callback still running after the source is freed touches only a live context. This closes the risk raised in review: Apple doesn't promise that removing a listener waits for an in-flight callback, and that callback used to dereferenceca.exit_eventis checked (os_event_try) after every wake;coreaudio_shutdownsignalsexit_eventand the wake event before the join. The success path (break oncoreaudio_initsuccess) and the rest of shutdown are the same.LOG_INFOline the first time the wait reaches the 30 s cap.build/mac-capture.pch.h(BUILD.md) and listed inCMakeLists.txt.Tests
New Tier 2-style cmocka test
test/cmocka/test_coreaudio_retry.c(+tests.d/test_coreaudio_retry.cmake, links only cmocka), simulated time, init always fails over 120 s:test_wake_outlives_source: listener ref, then source unref (no free), then a late "callback"coreaudio_wake_signal()finds a live context (reset set, event signalled), then the listener unref frees it (free count 1).test_wake_source_last: frees exactly once, on the last of three unrefs.The test links
OBS::libobs(the header uses libobsutil/bmem.handutil/threading.h, but still no Apple headers).RED (5f78668, stub returning the fixed interval),
rust/tools/linux-validate/run.sh --repeat 1 --tests test_coreaudio_retry:test_coreaudio_retry ... Failed(all 3 cases fail). GREEN:100% tests passed, 0 tests failed out of 295in both builds; repeat run passed. After the wake-context change:100% tests passed, 0 tests failed out of 296in both builds, and the repeat run passed.No existing C test or public header changed. Tiers 1/3/4 do not apply (no Rust port, C-only plugin bug fix).
What compiled the macOS code
PR CI does not compile
plugins/mac-captureC (the macOS lane is Rust cross lint only; app builds don't run from a PR path). Locally:OBS_CROSS_CMAKE_ARGS=-DOBS_CROSS_WERROR=ON cmake/cross/docker.sh mac-arm64(clang 21.1.5, macOS 14.5 SDK, arm64-apple-macos13.0):mac-capture-crossbuiltmac-capture.core-macos+.h.c.o(contains mac-audio.c.h, plus its PCH with coreaudio-retry.h) andmac-capture.sck-stop+.h.c.owith no warnings under -Werror. The overall cross run exited 1 only because of pre-existing-Wdouble-promotionerrors under -Werror in unrelated files on master (libobs obs-output/obs/obs-scene,mac-display-capture.m.h). Re-run after the wake-context commit (fix(mac-capture): reconnect listeners use a refcounted wake context): the incremental build rebuilt the mac-capture C PCH andmac-capture.core-macos+.h.c.owith no errors or warnings.mac-capture.sck-stop+.h.c.owas not rebuilt because it doesn't include the header. The run again exited 1, only for the same pre-existing errors (mac-display-capture.m.h, libobs graphics, mac-videotoolbox).