Skip to content

linux-v4l2: Open devices close-on-exec - #340

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/12508-v4l2-fd-cloexec
Oct 12, 2026
Merged

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

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator

Description

A V4L2 camera can now be restarted while OBS records. Before, changing its resolution, frame rate or format during a recording froze it ("Unable to set input 0", "Initialization failed, errno: Device or resource busy") until the recording stopped.

  • The plugin opened the camera without O_CLOEXEC. The recording output starts obs-ffmpeg-mux with posix_spawn() (os_process_pipe_create2() in libobs/util/pipe-posix.c), so the muxer got a copy of the camera's descriptor and kept it until it exited.
  • When the source restarts, v4l2_close() only drops OBS's own copy. The driver releases the device when the last copy is closed, which happened in os_process_pipe_destroy() when the recording stopped. That is where the reporter's gdb and uvcvideo traces put the release.
  1. test/cmocka: Add failing test for a V4L2 camera held by the muxer: test_v4l2_fd_cloexec runs the real v4l2-input.c against a fake libv4l2 whose v4l2_open() opens a FIFO with the flags the plugin passes, as libv4l2 does. A stand-in for the muxer is started the way obs-ffmpeg-mux is, through os_process_pipe_create2(args, "w"), and the test reads its open files from /proc/<pid>/fd. The cases are:

    • control: a descriptor opened without O_CLOEXEC shows up in the muxer;
    • a recording starts while the camera captures: the muxer must not hold the camera;
    • the steps of the report: start a recording, then change the resolution, which closes and reopens the camera. Afterwards the camera must be open once (by the source), and not at all in the muxer once the source is removed;
    • the properties open the camera for each list and for a control, and none of those opens may be inheritable.

    Today the muxer holds the camera, the camera is open twice after the restart, and 5 of 5 property opens are inheritable. 3 of 4 fail; the control passes.

  2. linux-v4l2: Open devices close-on-exec: adds O_CLOEXEC to every v4l2_open() in the plugin (capture, device list, property lists, controls). 4 of 4 pass.

Motivation and Context

Fixes #339 (upstream obsproject/obs-studio#12508, reported by stephematician). stephematician narrowed it down in the issue: v4l2_close() returns 0, the uvcvideo trace shows the device is released only when the recording stops, and gdb puts the release in os_process_pipe_destroy(). They also found that, during a recording, switching the source to a v4l2loopback device works but switching back to the webcam fails. That fits the cause: the webcam was open when the muxer started, and the loopback device was opened later. There is no upstream PR for it, and no code from upstream is used here.

This is not #203 / PR #204 (upstream obsproject/obs-studio#5422, with the fix stephematician opened as obsproject/obs-studio#13295). That was a missing close in OBS's own process. Here OBS closes the camera, but the muxer keeps a copy. It also does not touch #266 (the libobs pipes made close-on-exec in a second step): this PR leaves pipe-posix.c alone.

Why this one: changing a camera's settings mid-recording is ordinary. The camera froze with a "busy" error that points at the hardware, and nothing in OBS showed that its own recording held the device. The reporter tested several OBS builds, encoders and kernels before tracing it.

How Has This Been Tested?

Automated, in both libobs lanes (ENABLE_RUST_LIBOBS OFF and ON), on current master:

  • test_v4l2_fd_cloexec: 3 of 4 cases fail at the RED commit and 4 of 4 pass at GREEN. All four cases run in about 0.5 s, with and without a display (no obs_startup(), nothing to skip).
  • Full ctest at GREEN: 107 of 107 in each lane, also with the system's OpenGL module hidden.
  • libv4l2 1.32 passes O_CLOEXEC to openat() unchanged (checked under gdb), so the flag reaches the kernel through the real library too.
  • clang-format, gersemi and ci/test_registration.py are clean. The test is registered Linux-only and sets no working directory.

Not tested:

  • A real camera or the OBS UI. The test checks the cause (which process holds the device), not the driver's EBUSY.
  • Other descriptors the muxer inherits. The virtual camera (v4l2-output.c opens the loopback device with plain open()), the RTMP socket and VA-API probing are also opened without close-on-exec. They are left for separate changes, each with its own test.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

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.
  • I have used AI tooling in the creation of this PR
  • I am an AI agent being used to assist with this pull request: Claude Code (Claude Opus 5.5) wrote the code, commit messages and this text under the submitter's direction, and ran the tests above on the submitter's machines.

🤖 Generated with Claude Code

A V4L2 camera must be released when the source closes it, also while a
recording runs. The recording output starts obs-ffmpeg-mux with
posix_spawn(), and a child process gets a copy of every descriptor that
is not close-on-exec.

The test runs the real v4l2-input.c against a fake libv4l2 whose
v4l2_open() opens a FIFO with the flags the plugin passes, as libv4l2
does. A stand-in for the muxer is started like obs-ffmpeg-mux, through
os_process_pipe_create2(args, "w"), and the test reads its open files
from /proc/<pid>/fd. Cases:
- control: a descriptor opened without O_CLOEXEC shows up in the muxer;
- a recording starts while the camera captures: the muxer must not hold
  the camera;
- the steps of the report: start a recording, then change the
  resolution, which closes and reopens the camera; afterwards the camera
  must be open once (by the source) and not at all in the muxer once the
  source is removed;
- the properties open the camera for each list and for a control; none
  of those opens may be inherited by a spawned process.

On master the three cases fail: the muxer holds the camera, the camera
is open twice after the restart, and 5 of 5 property opens are
inheritable. The control passes. No camera, kernel module or display is
needed.

Reported upstream in obsproject/obs-studio#12508 by stephematician.

Refs obs-rust#339.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@rdoupe
rdoupe requested a review from zackees as a code owner October 12, 2026 07:44
While OBS recorded, a V4L2 camera could not be reopened: changing its
resolution or frame rate froze it with "Unable to set input 0 ...
Device or resource busy" until the recording stopped. The plugin opened
the camera without O_CLOEXEC, and the recording output starts
obs-ffmpeg-mux with posix_spawn(), so the muxer got a copy of the
camera's descriptor and kept it until it exited. v4l2_close() in OBS
then only dropped OBS's copy; the driver releases the device when the
last copy is closed, which was in os_process_pipe_destroy() when the
recording stopped, as the uvcvideo trace in the report shows.

Open every device the plugin opens with O_CLOEXEC: the capture itself,
the device list, the property lists and the camera controls. libv4l2
passes the flag to open(2) unchanged.

With the fix (both ENABLE_RUST_LIBOBS lanes), the muxer stand-in holds
no copy of the camera, the camera is open once after a restart during a
recording and not at all in the muxer after the source is removed, and
none of the property opens is inheritable. 4 of 4 cases pass (1 of 4
before).

Fixes obs-rust#339.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Local-Gate: v1 tree=d73eb83bd63373b0e0431f7bedaad8741f87d5eb secs=147 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791791584,"gate":"rust/all/fmt","host":"linux-x86_64","key":"260bebd184fc738080c6999970da47d6bf9618021fed6306ffcf2a69cbafb688","lane":"lint","parents":["79a540da588034d75e4991475ee9a8535dfcca42"],"secs":53,"stamp":"b1c50f8b6db1e26eeff1f29866160dce","tree":"d73eb83bd63373b0e0431f7bedaad8741f87d5eb","v":1,"via":"run"}
Ci-Attestation: {"at":1791791584,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"260bebd184fc738080c6999970da47d6bf9618021fed6306ffcf2a69cbafb688","lane":"lint","parents":["79a540da588034d75e4991475ee9a8535dfcca42"],"secs":53,"stamp":"0633355493bc9298ddfd423ed20fe71a","tree":"d73eb83bd63373b0e0431f7bedaad8741f87d5eb","v":1,"via":"run"}
Ci-Attestation: {"at":1791791584,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"7f18d53afe685a2cb776728f55e213bb54b81ac858e5e7361b0e17b397ba68d8","lane":"rust-test","parents":["79a540da588034d75e4991475ee9a8535dfcca42"],"secs":47,"stamp":"7a43c136bcabe4d083fb3c2d55f11b6a","tree":"d73eb83bd63373b0e0431f7bedaad8741f87d5eb","v":1,"via":"run"}
Ci-Attestation: {"at":1791791584,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"a39b365a67367addb96489b66397d14dc9eef88e200e23be96854194b3b1f03b","lane":"tier2-linux","parents":["79a540da588034d75e4991475ee9a8535dfcca42"],"secs":48,"stamp":"c82e799f417527d3cab02bb1a4a1b5e9","tree":"d73eb83bd63373b0e0431f7bedaad8741f87d5eb","v":1,"via":"run"}
@zackees
zackees force-pushed the fix/12508-v4l2-fd-cloexec branch from 8e81058 to 6ab13c9 Compare October 12, 2026 07:53
@zackees

zackees commented Oct 12, 2026

Copy link
Copy Markdown
Member

Verified locally, rebased onto master and stamped.

  • RED (79a540da5): Tier 2 passed 108 of 109. test_v4l2_fd_cloexec passed 1 of 4, with muxer_does_not_hold_the_capturing_camera, camera_is_released_when_restarted_while_recording and properties_open_the_camera_close_on_exec failing.
  • GREEN (8e8105856): 4 of 4 passed. Tier 2 passed 109 of 109 with ENABLE_RUST_LIBOBS OFF and 109 of 109 with it ON, and the local gate passed.

Merging once CI is green.

@zackees
zackees merged commit 0413fba into obs-rust:master Oct 12, 2026
23 checks passed
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 25, 2026
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 added a commit to rdoupe/obs-studio that referenced this pull request Oct 25, 2026
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
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 25, 2026
…st#2285)

* test: Virtual Camera device leaks into children

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

* linux-v4l2: Open the Virtual Camera close-on-exec

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
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: a camera stays busy while recording because the muxer inherits its fd (upstream #12508)

2 participants