Repository navigation
Description
Upstream OBS bug, reported by stephematician in obsproject/obs-studio#12508. Code unchanged in this fork and on upstream master.
Location
plugins/linux-v4l2/v4l2-input.cv4l2_init(): opens the camera withv4l2_open(data->device_id, O_RDWR | O_NONBLOCK), withoutO_CLOEXEC. The device list, the property callbacks (device_selected(),input_selected(),format_selected()viainput_caps_open(),resolution_selected()) andv4l2-controls.cv4l2_control_changed()open it the same way.libobs/util/pipe-posix.cos_process_pipe_create_internal(): startsobs-ffmpeg-muxfor a recording withposix_spawn(). The child gets a copy of every descriptor that is not close-on-exec, the camera included, and keeps it until it exits.
Class
Device stays busy: a camera cannot be reopened while OBS records.
How it is reached
Add a Video Capture Device (V4L2), start recording, then change anything that restarts the camera (resolution, frame rate, format, or switch to another device and back). The source closes the camera and opens it again. The reopen fails ("Unable to set input 0", "Initialization failed, errno: Device or resource busy") and the source stays frozen until the recording stops.
stephematician narrowed it down in the issue: v4l2_close() returns 0, the uvcvideo trace shows the device is only released when the recording stops, and gdb puts that release in os_process_pipe_destroy(). That is the muxer exiting and dropping its copy of the camera's descriptor. A driver releases the device only when the last copy is closed.
Checked with libv4l2 1.32: v4l2_open() passes its flags to openat() unchanged, so O_CLOEXEC reaches the kernel.
Not the same as
- bug: linux-v4l2: device left open when input caps can't be read, ending in "more than 16 video devices" (upstream #5422) #203 / PR linux-v4l2: Close the device when input caps cannot be read #204 (upstream error attempting to open more then 16 video devices obsproject/obs-studio#5422; the fix stephematician opened as linux-v4l2: Fix missing fd close obsproject/obs-studio#13295 while looking for this bug):
format_selected()left the device open when the input caps could not be read. That was a missing close in OBS's own process. Here OBS closes the camera, but the muxer still holds a copy. - bug: libobs/util: pipe-posix.c pipes are made close-on-exec non-atomically, so concurrent spawns inherit them #266: the libobs pipes in
pipe-posix.care made close-on-exec in a second step, so a concurrent spawn can inherit them. That is about OBS's own pipes, not about devices opened by plugins, and this issue does not changepipe-posix.c.
Proposed fix and failing test
Fix: open the device with O_CLOEXEC everywhere the plugin opens it (capture, device list, property lists, controls).
RED test (cmocka, test_v4l2_fd_cloexec, no camera, kernel module or display needed): the real v4l2-input.c runs against a fake libv4l2 whose v4l2_open() opens a FIFO with the flags the plugin passes. A stand-in for the muxer is started the way obs-ffmpeg-mux is (os_process_pipe_create2(args, "w")), and the test reads its open files from /proc/<pid>/fd. Cases:
- control: a descriptor opened without
O_CLOEXECshows 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, change the resolution, check that the camera is open once (by the source) and, after the source is removed, not at all in the muxer;
- the property opens (device, input, format, resolution, a control) must not be inheritable.
3 of 4 fail today; the control passes.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
rdoupe commentedon Oct 12, 2026
Claiming: this issue.
test_v4l2_fd_cloexec(3 of 4 failing): the real V4L2 source against a fake libv4l2, with a muxer stand-in spawned the way obs-ffmpeg-mux is; the muxer must not hold the camera, and a restart during recording must leave it open once.PR today.
linux-v4l2: Open devices close-on-exec
test/cmocka: Add failing test for a V4L2 camera held by the muxer
rdoupe commentedon Oct 12, 2026
@stephematician you reported this in obsproject/obs-studio#12508, so a heads-up: it's fixed here in obs-rust by #340, merged today with a regression test.
Cause: exactly what your gdb and uvcvideo traces pointed at. The V4L2 source opened the camera without close-on-exec, so when a recording started, the obs-ffmpeg-mux process OBS spawns inherited a copy of the camera's fd and held it until it exited.
v4l2_closeclosed OBS's copy, but the driver only releases the device when the last copy closes, so reopening it at a new resolution or frame rate failed with "Device or resource busy" until recording stopped. That also explains why switching to a loopback device worked but switching back didn't. Now every V4L2 open is close-on-exec, so the muxer never gets the camera. The test runs the real V4L2 source and spawns a muxer stand-in the same way OBS does: before, the muxer held the camera and a restart during recording left it open twice; now the muxer holds nothing.Not covered: tested with a simulated device, not a real camera; the virtual camera output opens its device a different way and isn't changed here.
Workaround on stock OBS: change the camera's resolution or frame rate only while not recording.
obs-rust is an AI-assisted fork of OBS; on Arch it's
obs-studio-rust-gitin the AUR.If you try it, a short reply here on whether changing the camera mid-recording works for you would really help.