Skip to content

linux-v4l2: Keep the virtual camera fd out of child processes - #2285

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/vcam-fd-cloexec
Oct 25, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/vcam-fd-cloexec

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #2278

What and why

On Linux, the Virtual Camera could not be restarted while OBS was recording
(upstream obsproject/obs-studio#14054).

try_connect() in plugins/linux-v4l2/v4l2-output.c.h opened the
v4l2loopback device without O_CLOEXEC. A recording starts obs-ffmpeg-mux
with os_process_pipe_create(), and that call closes only its own pipe fds
in the child. So the muxer got a copy of the device fd and kept it until
the recording ended. Stopping the Virtual Camera closed only OBS's copy.
With exclusive_caps=1, v4l2loopback then refused the next start because
the device still had a producer.

#340 fixed the same leak on the V4L2 input side. This PR does the same for
the output:

  • try_connect() and the loopback_module_available() probe (the non-Linux
    path) open the device with O_CLOEXEC.
  • vaapi_open_display_drm() in plugins/obs-ffmpeg/vaapi-utils.c.h leaked
    the VA-API encoder's render node into the muxer the same way. It now opens
    with O_CLOEXEC too.
  • rust/obs-util/src/platform/unix/pipe.rs: the comment said Command
    closes every non-stdio fd in the child. It does not. Like the C
    posix_spawn, it leaves open every inherited fd that lacks FD_CLOEXEC.
    The comment now says so.

New tests (in tests.d, like #340's test_v4l2_fd_cloexec):

  • test_v4l2_vcam_cloexec runs the real v4l2-output.c with ioctl()
    wrapped. The wrapper reads the device fd's FD_CLOEXEC flag at
    VIDIOC_QUERYCAP.

    • opened_device_is_close_on_exec calls try_connect() on a temporary
      file. It needs no device, display or kernel module, so it runs in CI.
    • virtualcam_restarts_while_a_child_runs starts the Virtual Camera on
      vivid's video output node, starts a child with
      os_process_pipe_create(), then stops and restarts the camera while the
      child lives. It reads /proc/<pid>/fd for the child and for OBS.
      • It skips without a vivid output node (modprobe vivid node_types=0x100, as root) or without core video.
      • vivid has no VIDIOC_S_PARM for its output (v4l2loopback has one), so
        the wrapper answers that single ioctl for the vivid node.
      • virtualcam_start() is not called, because without v4l2loopback it
        runs pkexec modprobe.
  • test_vaapi_open_cloexec runs the real vaapi-utils.c with
    vaGetDisplayDRM() wrapped. Its cases:

    • vaapi_open_device() on /dev/null (no GPU needed);
    • the encoder probe on the first render node;
    • a child started while the encoder's device is open.

    The two render-node cases skip without a render node.

RED → GREEN

Both lanes use the build4 tree: build-c, and build-rust (hybrid, with
OBS_BUILD_C_REFERENCE=ON). Tests ran under a private Xvfb and D-Bus, with
a fresh HOME. vivid was loaded with node_types=0x100, and renderD128 was
present.

  • RED commit: 39daea0 (rebased; first pushed as 3a0995d, where these
    runs were made). On both lanes all 5 cases fail:
    • opened_device_is_close_on_exec: "try_connect() opened the device
      without O_CLOEXEC".
    • virtualcam_restarts_while_a_child_runs: copies of the vivid node were
      1 in a child while running, 1 after stop, 2 after restart, and 1 in a
      child started after the restart.
    • device_is_opened_close_on_exec and
      probe_opens_render_nodes_close_on_exec: "1 of 1 device opens lack
      O_CLOEXEC".
    • encoder_device_is_not_inherited: the child holds 1 copy of
      renderD128.
  • GREEN: 7b9015f. On both lanes, at e572bc851 and again after the rebase:
    • test_v4l2_vcam_cloexec passes 2/2. The copies are 0 while running, 0
      after stop, 1 after restart (OBS's own), and 0 in the second child.
    • test_vaapi_open_cloexec passes 3/3.
    • With vivid unloaded, or with no display, the vivid case skips and the
      FD_CLOEXEC case still passes. The exit code is 0.
    • linux-v4l2 and obs-ffmpeg build on both lanes.
  • Local gate at e572bc851: --test-diff origin/master (Rust workspace
    3904/3904, 6 skipped), --lane lint and --lane cross all ok. --lane lint was run again on 7b9015f and is ok. libobs is unchanged, so the
    exported symbols are unchanged.
  • Real app: not run. A real check needs v4l2loopback, which this host does
    not have. The vivid case drives the real output code through the same
    start, stop and restart sequence.

Checklist

  • Base is master
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits
  • User-visible change: its line in docs/features.md is added (Fixed upstream bugs, Video capture)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes: not applicable
  • Intentional differences from C: not applicable (no port)

Note for reviewers

After the final rebase, the vivid case of test_v4l2_vcam_cloexec failed once on the hybrid build, in the first run after a rebuild. Its error message was not captured. 135 later runs passed, including under full CPU load and with vivid reloaded before each run. The CI case (temp file, no device) is unaffected.

@obs-rust-project-manager obs-rust-project-manager Bot moved this to In progress in obs-rust Multiview Oct 25, 2026
@rdoupe
rdoupe force-pushed the fix/vcam-fd-cloexec branch from 3a0995d to 7b9015f Compare October 25, 2026 01:28
@rdoupe
rdoupe marked this pull request as ready for review October 25, 2026 01:28
@rdoupe
rdoupe requested a review from zackees as a code owner October 25, 2026 01:28

@zackees zackees left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coordinator review: correct and minimal. All three open() sites gain O_CLOEXEC (vcam try_connect, loopback probe, VA-API render node), matching #340's input-side fix. RED commit precedes GREEN; both new cmocka tests are wired via tests.d, the device-free case runs in CI without vivid. Features ledger line present; the pipe.rs comment correction is accurate. No public header or existing C test touched. Approve once the pending checks finish green.

@zackees zackees left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coordinator review (comment; same account as the author, so no approval possible): correct and minimal. All three open() sites gain O_CLOEXEC (vcam try_connect, loopback probe, VA-API render node), matching #340's input-side fix. RED precedes GREEN; both new cmocka tests are wired via tests.d, and the device-free case runs without vivid. Ledger line present; the pipe.rs comment fix is accurate. No public header or existing C test touched. LGTM once pending checks are green.

@zackees
zackees enabled auto-merge October 25, 2026 01:41
A Virtual Camera cannot be restarted while OBS records
(obsproject/obs-studio#14054). try_connect() in v4l2-output.c opens the
v4l2loopback device without O_CLOEXEC, and the recording starts
obs-ffmpeg-mux with os_process_pipe_create(), which closes only its own
pipe descriptors in the child. The muxer keeps a copy of the device for
the whole recording, so stopping the Virtual Camera does not release it
and v4l2loopback with exclusive_caps=1 refuses the next start.
vaapi_open_display_drm() leaks the encoder's render node the same way.

test_v4l2_vcam_cloexec runs the real v4l2-output.c with ioctl()
wrapped to read the FD_CLOEXEC flag of the device at VIDIOC_QUERYCAP:
- opened_device_is_close_on_exec: try_connect() on a temporary file,
  so no device, display or module is needed;
- virtualcam_restarts_while_a_child_runs: the Virtual Camera on vivid's
  video output node (node_types bit 8), a child started with
  os_process_pipe_create() while it runs, then stop and restart while
  the child lives. Skips without vivid or a display.

test_vaapi_open_cloexec runs the real vaapi-utils.c with
vaGetDisplayDRM() wrapped: vaapi_open_device() on /dev/null (no GPU),
the encoder probe on the first render node, and a child started while
the encoder's device is open. The render node cases skip without one.

On master all five cases fail.

Refs obs-rust#2278
The Virtual Camera could not be restarted while OBS recorded
(obsproject/obs-studio#14054). try_connect() opened the v4l2loopback
device without O_CLOEXEC, and the recording starts obs-ffmpeg-mux with
os_process_pipe_create(), which closes only its own pipe descriptors in
the child. The muxer kept a copy of the device until the recording
ended: stopping the Virtual Camera closed only OBS's copy, and
v4l2loopback with exclusive_caps=1 refused the next start because the
device still had a producer. obs-rust#340 fixed the same leak for the V4L2
input.

Open the device with O_CLOEXEC in try_connect() and in the
loopback_module_available() probe (the non-Linux path).
vaapi_open_display_drm() leaked the VA-API encoder's render node into
the muxer the same way; it opens with O_CLOEXEC too.

The Rust pipe port's comment said Command closes every non-stdio fd in
the child. It does not: like posix_spawn in the C, it leaves every
inherited fd without FD_CLOEXEC open. The comment says so now.

With the fix, on both ENABLE_RUST_LIBOBS lanes (build-c, and the
hybrid build-rust): test_v4l2_vcam_cloexec passes 2 of 2. On vivid's
output node, a child started while the camera runs holds no copy, the
device is closed everywhere after stop, and it is open once after a
restart while that child lives. test_vaapi_open_cloexec passes 3 of 3
on renderD128. Before the fix, all 5 cases failed.

Fixes obs-rust#2278
@rdoupe
rdoupe force-pushed the fix/vcam-fd-cloexec branch from 7b9015f to 04e3aaf Compare October 25, 2026 01:45
@zackees
zackees added this pull request to the merge queue Oct 25, 2026
Merged via the queue into obs-rust:master with commit 6c25992 Oct 25, 2026
35 checks passed
@obs-rust-project-manager obs-rust-project-manager Bot moved this from In progress to Done in obs-rust Multiview Oct 25, 2026
@rdoupe rdoupe mentioned this pull request Nov 1, 2026
4 of 7 tasks
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.

bug: linux-v4l2: Virtual Camera can't restart during a recording because the muxer inherits its device fd (upstream #14054)

2 participants