Skip to content

mac-capture: CoreAudio reconnect backs off instead of retrying every 300 ms / 2 s forever (#1358) - #1370

Merged
zackees merged 4 commits into
masterfrom
fix/1358-coreaudio-reconnect-backoff
Oct 18, 2026
Merged

zackees merged 4 commits into
masterfrom
fix/1358-coreaudio-reconnect-backoff

Conversation

@zackees

@zackees zackees commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

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.h reconnect thread: while (os_event_timedwait(exit_event, retry_time) == ETIMEDOUT) if (coreaudio_init(ca)) break; with retry_time 300 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

  • New pure header plugins/mac-capture/coreaudio-retry.h (no Apple headers): coreaudio_retry_reset(r, base) / coreaudio_retry_next(r, &cap_first). Waits start at the existing retry_time, double, cap at 30 s; cap_first is true once per schedule.
  • While reconnecting, the thread registers a system-object listener on kAudioHardwarePropertyDevices and the default-device property (the per-device listeners are removed while the device is down, so the existing notification_callback cannot fire then). The callback wakes the thread, which restarts the schedule at its base.
  • Late-callback safety: the listener's refcon is a small heap struct coreaudio_wake (atomic refcount, auto wake event, reset flag) in coreaudio-retry.h, never ca. The callback only calls coreaudio_wake_signal(). The source holds one reference (created in coreaudio_create), and each successfully registered listener holds another, dropped only after AudioObjectRemovePropertyListener returns. The last coreaudio_wake_unref() destroys the event and frees the context, so coreaudio_destroy no 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 dereference ca.
  • exit_event is checked (os_event_try) after every wake; coreaudio_shutdown signals exit_event and the wake event before the join. The success path (break on coreaudio_init success) and the rest of shutdown are the same.
  • One LOG_INFO line the first time the wait reaches the 30 s cap.
  • Header included through build/mac-capture.pch.h (BUILD.md) and listed in CMakeLists.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:

  • 300 ms base: 300, 600, ... 19200, then 30000 capped; cap reported once.
  • 2 s base: 2000, 4000, 8000, 16000, 30000...; cap reported once.
  • reset after the cap returns to the base and doubles again; cap reported again.
  • 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 libobs util/bmem.h and util/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 295 in both builds; repeat run passed. After the wake-context change: 100% tests passed, 0 tests failed out of 296 in 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-capture C (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-cross built mac-capture.core-macos+.h.c.o (contains mac-audio.c.h, plus its PCH with coreaudio-retry.h) and mac-capture.sck-stop+.h.c.o with no warnings under -Werror. The overall cross run exited 1 only because of pre-existing -Wdouble-promotion errors 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 and mac-capture.core-macos+.h.c.o with no errors or warnings. mac-capture.sck-stop+.h.c.o was 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).

…interval (#1358)

Adds a pure retry-schedule header (stub reproducing today's fixed 300 ms /
2 s interval) and a cmocka test driving it in simulated time. Fails: the
schedule never backs off or caps, and a reset does not restart doubling.

Refs #1358
…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
zackees force-pushed the fix/1358-coreaudio-reconnect-backoff branch from 95e22b6 to 3ba49f8 Compare October 18, 2026 00:39
@zackees
zackees enabled auto-merge October 18, 2026 00:39
@zackees
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
@zackees
zackees enabled auto-merge October 18, 2026 01:13
@zackees
zackees added this pull request to the merge queue Oct 18, 2026
Merged via the queue into master with commit a7e0d9d Oct 18, 2026
22 checks passed
@zackees
zackees deleted the fix/1358-coreaudio-reconnect-backoff branch October 26, 2026 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant