Repository navigation
linux-v4l2: Open devices close-on-exec - #340
Merged
Merged
Conversation
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]>
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
force-pushed
the
fix/12508-v4l2-fd-cloexec
branch
from
October 12, 2026 07:53
8e81058 to
6ab13c9
Compare
Member
|
Verified locally, rebased onto master and stamped.
Merging once CI is green. |
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
5 of 7 tasks
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
O_CLOEXEC. The recording output startsobs-ffmpeg-muxwithposix_spawn()(os_process_pipe_create2()inlibobs/util/pipe-posix.c), so the muxer got a copy of the camera's descriptor and kept it until it exited.v4l2_close()only drops OBS's own copy. The driver releases the device when the last copy is closed, which happened inos_process_pipe_destroy()when the recording stopped. That is where the reporter's gdb and uvcvideo traces put the release.test/cmocka: Add failing test for a V4L2 camera held by the muxer:test_v4l2_fd_cloexecruns the realv4l2-input.cagainst a fake libv4l2 whosev4l2_open()opens a FIFO with the flags the plugin passes, as libv4l2 does. A stand-in for the muxer is started the wayobs-ffmpeg-muxis, throughos_process_pipe_create2(args, "w"), and the test reads its open files from/proc/<pid>/fd. The cases are:O_CLOEXECshows up in the muxer;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.
linux-v4l2: Open devices close-on-exec: addsO_CLOEXECto everyv4l2_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 inos_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.calone.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_LIBOBSOFF 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 (noobs_startup(), nothing to skip).O_CLOEXECtoopenat()unchanged (checked under gdb), so the flag reaches the kernel through the real library too.ci/test_registration.pyare clean. The test is registered Linux-only and sets no working directory.Not tested:
v4l2-output.copens the loopback device with plainopen()), 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
Checklist:
🤖 Generated with Claude Code