Skip to content

linux-v4l2: recover a camera another process holds (EBUSY): name it, Force Camera, auto-retry - #800

Merged
zackees merged 7 commits into
masterfrom
fix/799-v4l2-busy-camera
Oct 15, 2026
Merged

zackees merged 7 commits into
masterfrom
fix/799-v4l2-busy-camera

Conversation

@zackees

@zackees zackees commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

Fixes #799

A camera another process holds open (EBUSY) used to leave the v4l2_input source dead, with only a log line. Seen with a stock OBS left running that held /dev/video0, so OBS-Studio-Rust showed no camera.

What changes

New plain-C unit plugins/linux-v4l2/v4l2-busy.c, wired into v4l2-input.c:

  1. Names the holder. It finds the process holding the device by scanning /proc/<pid>/fd, resolving symlinks such as /dev/v4l/by-id. The log and a warning at the top of the source properties say "/dev/video0 is in use by obs (PID 1234)". Another OBS instance is called out as the common case.
  2. Force Camera button. The button is shown only when the holder belongs to the same user. It sends SIGTERM, waits up to 5 s for the device to be released, then opens it. It never escalates to SIGKILL, and a timeout or a holder owned by another user is logged.
  3. Automatic retry. A busy source rechecks once a second on the UI thread (the check is queued from video_tick) and opens the device as soon as the holder lets go. When the holder can't be seen (it belongs to another user), it retries with a backoff from 2 s up to 30 s.

Tests

  • RED 7f93686b5: test_v4l2_busy fails, 9/11 cases.
  • GREEN: local tier2-linux lane: 234/234 pass, both OFF and ON.

test_v4l2_busy covers:

  • a fake /proc tree, including excluding OBS's own pid and a by-id symlink;
  • a real forked helper process holding a device node: it is found in the real /proc, SIGTERM releases it, a holder owned by another uid is refused, and a helper that ignores SIGTERM times out and is not killed;
  • the retry backoff.

The C sources of the existing v4l2 tests were not edited. Their tests.d CMake files that compile v4l2-input.c now also compile v4l2-busy.c.

Popup ("Camera is in use" + Force Camera)

  • The v4l2 source emits the global signal source_device_busy (source, device, holder name and PID, is_obs, can_force) once per busy episode. It also registers the proc force_camera(out bool released).
  • The frontend (OBSBasic_DeviceBusy.cpp) declares the signal before plugins load and shows a modal Camera is in use dialog that names the holder. The dialog has a Force Camera button, shown only for a process of the same user, which calls the proc and reports when the holder did not close. Only one popup per device is shown at a time.
  • RED test_v4l2_busy_signal: 3/4 cases failed. GREEN: local tier2-linux lane passes 235/235, OFF and ON. The test runs the real v4l2-input.c on a real libobs source with forked holders, and checks that the signal fires once, that force_camera closes the holder with SIGTERM and reopens the device, that a holder that refuses is not killed, and the auto-retry through video_tick.
  • The frontend is not built by the local gate; PR CI's full builds compile it.

… retry (#799)

test_v4l2_busy pins the new plain-C unit v4l2-busy.c against stubs: find
the process holding the device in a fake /proc tree and in the real /proc
(a forked helper holding a device node), SIGTERM it only when it belongs to
the same user, never escalate to SIGKILL, and back off retries 2 s -> 30 s.

Refs #799
When opening or setting up the device fails with EBUSY, the source finds the
process holding it (/proc/<pid>/fd), logs and shows it in the properties
("/dev/video0 is in use by obs (PID 1234)", calling out another OBS
instance), and offers a Force Camera button for a holder of the same user.
Force Camera sends SIGTERM, waits up to 5 s for the device to be released and
opens it; it never escalates to SIGKILL. A busy source rechecks once a second
on the UI thread and opens the device as soon as the holder lets go, or on a
2 s -> 30 s backoff when the holder cannot be seen (another user).

Fixes #799
test_v4l2_busy_signal runs the real v4l2-input.c on a real libobs source
against a fake libv4l2 that is busy while a forked helper holds the device.
It expects one global source_device_busy signal naming the holder (for the
frontend's popup), a force_camera proc that closes the holder with SIGTERM
and opens the camera, no SIGKILL for a holder that refuses, and a retry
from video_tick once the holder exits.

Refs #799
The v4l2 source emits the global signal source_device_busy (source, device,
holder name and PID, whether it is another OBS, whether it may be closed)
once per busy episode, and registers the proc force_camera(out bool
released). The frontend declares the signal before plugins load and shows a
modal "Camera is in use" dialog naming the holder, with a Force Camera
button (only for a process of the same user) that calls the proc and
reports when the holder did not close. One popup per device at a time.

Fixes #799
@zackees
zackees force-pushed the fix/799-v4l2-busy-camera branch from 1c279e2 to 1a7a805 Compare October 15, 2026 05:35
@zackees zackees added the ci-full Run every CI job on this PR, including the release-gating OBS app builds and Flatpak label Oct 15, 2026
…ncation) (#799)

CI's GCC flags snprintf of <proc>/<pid>/fd/<name> into a PATH_MAX buffer as
possibly truncated. Check the length and skip such entries.

Refs #799
…799)

-Werror=format-nonliteral in the full build: fill %1/%2 with dstr_replace
instead of passing the locale string to dstr_printf.

Refs #799
@zackees
zackees added this pull request to the merge queue Oct 15, 2026
Merged via the queue into master with commit 1c999d1 Oct 15, 2026
25 checks passed
@zackees
zackees deleted the fix/799-v4l2-busy-camera branch October 26, 2026 15:44
zackees added a commit that referenced this pull request Nov 6, 2026
…ements (#3724)

OBSCameraBusy names the device and every holder and waits for a click:
Close it, Ignore, Always allow. It is a child of the main window (its
transient parent), window type exactly Qt::Dialog, non-modal, never on top.
Two traps the Qt test caught: masking out Qt::Tool clears the Dialog type
bits too (Tool is a type, not a flag), which made the window a child widget
inside the main window; and QDialogButtonBox makes its first AcceptRole
button the default on show, so Enter answered Always allow. Always allow is
an ActionRole button for that reason, and no button is auto-default.

CameraBusy scans /proc off the UI thread, asks rust/obs-camera-busy whether
to show, logs every warning and answer, and runs Close it on a worker
thread. In the background the window shows without activating and the
taskbar entry is alerted; while the main window is hidden or minimized the
warning waits for it. The C rollback build keeps #800's popup.

linux-v4l2 announces source_device_opened/closed so the frontend can watch
the node for other programs opening it while OBS holds it. The existing
v4l2 C tests are unchanged; an undeclared signal is a no-op there.
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Nov 6, 2026
…ery holder, later opens (obs-rust#3724) (obs-rust#3725)

* obs-camera-busy: RED: the camera-busy warning's core as todo!() stubs (obs-rust#3724)

Tier 1 tests for the decision core of the camera-busy warning: holder
scan over a fixture /proc tree, display names, the allowlist, dedupe and
rate limit, what an answer does, Close it's SIGTERM -> SIGKILL sequence,
the log lines, the inotify watcher and the C ABI. They run as a module of
obs-tests-gpu, the frontend's Rust test category, like
obs-fallback-warning's.

RED: 39 of 40 fail on todo!(); the ffi module aborts the binary (a
todo!() cannot unwind out of extern "C").

* obs-camera-busy: GREEN: holders, decisions, Close it and the watcher (obs-rust#3724)

The core of the camera-busy warning. Holders come from the fd links of a
/proc tree under a caller-supplied root. A holder's name is its exe's file
name unless comm disagrees: comm is the exe name cut to 15 bytes, so when it
is not a prefix the binary is multicall (NixOS's sleep is coreutils) or it
renamed itself, and comm is what ps shows. Nix's .foo-wrapped is foo.

One dialog per device and app at a time, then 60 s of quiet after an
answer. An open that nobody holds 500 ms later (OBS reopening its own
camera, a browser probing every /dev/video*) is not reported. Close it
sends SIGTERM, then SIGKILL after 2 s, by the maintainer's decision on
obs-rust#3724 (obs-rust#799 had said SIGTERM only; the properties-panel Force Camera
button keeps that). A zombie counts as gone: an exited child holds no fd.

The crate links into the frontend through obs-frontend, not libobs, so it
needs no rust-exports.d/rust-hidden.d fragment and Tier 2 never builds it
(local-gate.toml exclude). Its platform/mod.rs is a new crate's shared
platform boundary (ci/shared-build-files.txt).

46 camera_busy tests pass in obs-tests-gpu.

* frontend, linux-v4l2: the camera-busy window, glue and device announcements (obs-rust#3724)

OBSCameraBusy names the device and every holder and waits for a click:
Close it, Ignore, Always allow. It is a child of the main window (its
transient parent), window type exactly Qt::Dialog, non-modal, never on top.
Two traps the Qt test caught: masking out Qt::Tool clears the Dialog type
bits too (Tool is a type, not a flag), which made the window a child widget
inside the main window; and QDialogButtonBox makes its first AcceptRole
button the default on show, so Enter answered Always allow. Always allow is
an ActionRole button for that reason, and no button is auto-default.

CameraBusy scans /proc off the UI thread, asks rust/obs-camera-busy whether
to show, logs every warning and answer, and runs Close it on a worker
thread. In the background the window shows without activating and the
taskbar entry is alerted; while the main window is hidden or minimized the
warning waits for it. The C rollback build keeps obs-rust#800's popup.

linux-v4l2 announces source_device_opened/closed so the frontend can watch
the node for other programs opening it while OBS holds it. The existing
v4l2 C tests are unchanged; an undeclared signal is a no-op there.

* linux-v4l2: announce the device only for a source libobs runs; camera_busy fixture tests on Linux only (obs-rust#3724)

The v4l2 C tests create the source with no obs_source_t and libobs not
started, and obs_get_signal_handler() dereferences the unstarted core:
every test_v4l2_* that opens the fake camera segfaulted (and asan then
reported the leaks of the dead process). Check the source first, as there
is nobody to tell without one.

The /proc fixture tests expect can_close, which needs the owner uid that
only the Linux platform reads; macOS has no V4L2 and reported false.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-full Run every CI job on this PR, including the release-gating OBS app builds and Flatpak

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

linux-v4l2: recover when the camera is held by another process (EBUSY): name the holder, offer to close it, auto-retry

1 participant