Skip to content

bug: linux-v4l2: a camera stays busy while recording because the muxer inherits its fd (upstream #12508) #339

Closed

Description

@rdoupe

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.c v4l2_init(): opens the camera with v4l2_open(data->device_id, O_RDWR | O_NONBLOCK), without O_CLOEXEC. The device list, the property callbacks (device_selected(), input_selected(), format_selected() via input_caps_open(), resolution_selected()) and v4l2-controls.c v4l2_control_changed() open it the same way.
  • libobs/util/pipe-posix.c os_process_pipe_create_internal(): starts obs-ffmpeg-mux for a recording with posix_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

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_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, 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.

Activity

  1. rdoupe commented on Oct 12, 2026

    @rdoupe
    CollaboratorAuthor

    Claiming: this issue.

    1. RED: 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.
    2. GREEN: open V4L2 devices close-on-exec.
      PR today.
  2. added 2 commits that reference this issue on Oct 12, 2026
    6ab13c9
    79a540d
  3. rdoupe commented on Oct 12, 2026

    @rdoupe
    CollaboratorAuthor

    @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_close closed 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-git in the AUR.

    If you try it, a short reply here on whether changing the camera mid-recording works for you would really help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions