Skip to content

V4L2 input does not close while recording #12508

Open

Description

@stephematician

Operating System Info

Ubuntu 24.04

Other OS

No response

OBS Studio Version

Git

OBS Studio Version (Other)

No response

OBS Studio Log URL

https://obsproject.com/logs/Ombuu5dUBteEJ6AN

OBS Studio Crash Log URL

No response

Expected Behavior

Should be able to start and stop V4L2 input while recording, e.g. when changing resolution of the camera.

Current Behavior

Freezes after the resolution is changed.

Steps to Reproduce

  1. Add a "Video Capture Device (V4L2)" to the scene, select a webcam.
  2. Activate Start recording.
  3. Open properties of the "Video Capture Device (V4L2)" source, and make any change that requires camera restart (e.g. frame rate, resolution, etc).

The device freezes.

The device appears to be closed once Stop recording is activated and can then be re-activated.

When not recording, the device stops and starts without problem.

Anything else we should know?

This goes as far back as 30.2.3 at least; haven't tested further back as it gets more difficult to build. Maybe this is a dependency issue or perhaps something in certain kernels - it seems odd that the file (descriptor) isn't cleaned up properly by the call to v4l2_close().

Would like to confirm if others have experienced the same issue.

Currently running 6.14.0-27 generic on Ubuntu 24.04 (HWE kernel line).

Activity

  1. stephematician commented on Aug 15, 2025

    @stephematician
    ContributorAuthor

    Forgot to mention; when using x264 encoder - the camera can be stopped and started no problem.

  2. stephematician commented on Aug 15, 2025

    @stephematician
    ContributorAuthor

    Just tried strace -e trace=open,openat,close; and now it seems that it isn't encoder related. So that's a false flag. Also I can see that the close and open operations are succeeding.

    I tried adding a pause in before attempting to re-open the camera while recording, but it still fails.

    I think it's going to take someone smarter than me to figure this one out.

  3. changed the title [-]V4L2 input does not close while recording with NVIDIA NVENC and FFMPEG VAAPI[/-] [+]V4L2 input does not close while recording[/+] on Aug 15, 2025
  4. stephematician commented on Aug 16, 2025

    @stephematician
    ContributorAuthor

    Also occurs on 6.8.0-71-generic; so that means I've tried different OBS builds, different encoders, and different kernels.

    I'm still no closer to seeing where the bug comes from.

    [EDIT]

    I can start and stop a V4L2 loopback device without problem; i.e.:

    1. Start a loopback device with video_nr=10 max_buffers=4 and feed it with gstreamer video test source:
      gst-launch-1.0 videotestsrc horizontal_speed=4 ! \
          'video/x-raw,width=640,height=480,framerate=30/1' ! \
          videoconvert ! 'video/x-raw,format=YUY2,framerate=30/1' ! \
          v4l2sink device=/dev/video10
      
    2. Start recording in OBS studio with the webcam selected in the Video Capture Device (V4L2) source.
    3. Open the properties (while recording), select the loopback (test feed) which is /dev/video10 and it starts ok.
    4. Try to switch back to the webcam; it fails:
      info: v4l2-input: /dev/video10: Stopped capture after 40 frames
      info: v4l2-input: Start capture from /dev/video0
      error: v4l2-input: Unable to set input 0
      error: v4l2-input: Initialization failed, errno: Device or resource busy
      
    5. If I switch back to the loopback; it starts no problem.

    Again, can only restart the camera once I have stopped recording.

    I used modprobe uvcvideo trace=0xFFFF and here it looks like the device isn't released (e.g. a trace of uvc_v4l2_release) until the recording is stopped - this is in spite of the fact that I'm certain that v4l2_close is being called (and returning 0) when the camera resolution is changed.

    [/EDIT]

  5. stephematician commented on Aug 16, 2025

    @stephematician
    ContributorAuthor

    Using GDB; I'm seeing that the device is finally released here:

    #0  os_process_pipe_destroy at obs-studio/libobs/util/pipe-posix.c:146
    fclose(pp->file);
    
  6. stephematician commented on Apr 6, 2026

    @stephematician
    ContributorAuthor

    @hoshinolina I noticed your recent PR #13198 addressed some unusual fd close behaviour, do you know if anything you found there could be related?

  7. stephematician commented on Oct 12, 2026

    @stephematician
    ContributorAuthor

    This is due to leaking fd's into a spawned process, which can happen via

    int ret = posix_spawn(&pid, bin, &file_actions, NULL, (char *const *)argv, environ);

    The question is what is the correct fix (open the V4L2 device with O_CLOEXEC, or something else?)

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

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions