Skip to content

mac-avcapture: Lock device outside session config#13427

Closed
2opremio wants to merge 1 commit into
obsproject:masterfrom
2opremio:mac-avcapture-balance-config
Closed

mac-avcapture: Lock device outside session config#13427
2opremio wants to merge 1 commit into
obsproject:masterfrom
2opremio:mac-avcapture-balance-config

Conversation

@2opremio
Copy link
Copy Markdown

@2opremio 2opremio commented May 12, 2026

Description

Move lockForConfiguration: out of the session config batch in configureSession. The lock isn't a session operation and the old order leaked an unbalanced beginConfiguration on lock failure, which eventually crashed OBS on startRunning.

Motivation and Context

lockForConfiguration: can fail on a USB hot-plug while the device is still settling. With the lock inside beginConfiguration / commitConfiguration, the early return on failure left the session mid-config. The next deviceConnected: opens more nested begin/commit pairs but the outer one stays uncommitted, then startRunning throws:

If you've called -beginConfiguration, you must call -commitConfiguration before invoking -startRunning or -stopRunning, otherwise an NSGenericException is thrown.

(AVCaptureSession.h)

OBS doesn't catch it and aborts. Repros frequently on a Mac Studio M1 Ultra (macOS 26.4.1, OBS 32.1.2) with a camera on a switched USB hub.

Log: https://obsproject.com/logs/8S3wBZPQ2vtcOcmB
Crash: https://obsproject.com/logs/crashes/R51BD5uJRNxyCSqe

Supersedes #13426.

How Has This Been Tested?

Building locally. Will update before marking ready for review.

Types of changes

  • Bug fix

Checklist

  • I have read the contributing document.
  • My code has been run through clang-format.
  • My code follows the project's style guidelines.
  • My code is not on the master branch.
  • My code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.

configureSession calls -[AVCaptureSession beginConfiguration] before
-[AVCaptureDevice lockForConfiguration:], and returns NO without a
matching commitConfiguration when the lock fails. lockForConfiguration
can fail transiently during a USB hot-plug while AVFoundation is still
settling the newly enumerated device.

On the next deviceConnected: notification, switchCaptureDevice and
configureSession run more begin/commit pairs, but each begin requires
its own commit, so the outermost begin from the previous failed
attempt stays uncommitted. When startCaptureSession is finally
dispatched, -[AVCaptureSession startRunning] raises NSGenericException
per Apple's documented invariant:

  If you've called -beginConfiguration, you must call
  -commitConfiguration before invoking -startRunning or -stopRunning,
  otherwise an NSGenericException is thrown.

  -- AVFoundation/AVCaptureSession.h

Nothing in OBS catches the exception, so OBS aborts via
_objc_terminate. Reproduces frequently on macOS 26.4.1 / OBS 32.1.2
with a camera on a switched USB hub powering on while OBS is running.

Move lockForConfiguration before beginConfiguration. The lock
operates on the AVCaptureDevice, not the session, so it doesn't
belong inside the session configuration batch. The early return
on lock failure now happens before any begin, keeping begin/commit
balanced on every path.
@2opremio
Copy link
Copy Markdown
Author

For anyone else hitting this crash: the change does seem to fix it (tested with this PR's CI artifact). See #13426 for context on why this was closed.

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.

2 participants