Skip to content

linux-v4l2: Close the device when input caps cannot be read - #204

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/203-v4l2-caps-fd-leak
Oct 11, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/203-v4l2-caps-fd-leak

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator

Description

A V4L2 Video Capture Device source leaked an open device every time its properties were refreshed on a device whose input capabilities can't be read (VIDIOC_ENUMINPUT fails, as on ISP/codec nodes, e.g. on a Raspberry Pi). libv4l2 tracks at most 16 open devices per process, so after 16 leaks every open fails with libv4l2: error attempting to open more than 16 video devices and no camera can be opened until OBS restarts. With this change the device is closed on that path.

The cause: format_selected() returned without v4l2_close(dev) when v4l2_get_input_caps() failed (plugins/linux-v4l2/v4l2-input.c:673-674). Every other early return in the plugin closes the device.

  1. test/cmocka: Add RED V4L2 input caps device leak tests: moves the open + caps query, unchanged, into v4l2_open_input_caps() in plugins/linux-v4l2/v4l2-input-caps.[ch] (open, close and the caps query are injected; format_selected uses it) and adds test_v4l2_input_caps, whose fake open copies libv4l2's 16-device limit. 2 of its 4 tests fail on this commit:
    • caps_query_failure_closes_device: "failed caps query left 1 device(s) open (1 opens, 0 closes)"
    • repeated_caps_query_failures_keep_opening: "refresh 17 could not open the device: 16 devices still open"
    • the success-path and open-failure tests pass.
  2. linux-v4l2: Close the device when input caps cannot be read: closes the device before returning. All 4 tests pass.

Motivation and Context

Fixes #203 (upstream obsproject/obs-studio#5422, reported by jclab-joseph and hit by gruzilla, ethagnawl, vicary and mh5). The fix is the one jclab-joseph proposed in the issue and stephematician opened as obsproject/obs-studio#13295 (approved upstream, not merged); both are credited as co-authors.

Why this one: five people hit it, it ends with no camera working until a restart, the issue has been open since 2021, and the fix is one line that matches every other error path in the plugin.

How Has This Been Tested?

Automated: test_v4l2_input_caps fails 2 of 4 at the RED commit and passes 4 of 4 at the GREEN commit; the linux-v4l2 plugin builds cleanly; clang-format is clean.

By hand, headless, with the real linux-v4l2.so in libobs on a v4l2loopback device (in a sandbox where only that device was visible), whose source settings name an input the device doesn't have, so VIDIOC_ENUMINPUT fails. Each iteration opens the properties, fires the pixel-format modified callback and destroys the properties:

before after
device descriptors left open 4 per properties dialog (0 → 16) 0 over 40 dialogs
process descriptors 9 → 25 9 → 9
libv4l2 refusing the device from the 4th dialog on; "more than 16 video devices" printed 113 times never

Not tested:

  • A real ISP/codec node (Raspberry Pi); the failure was produced by naming a missing input on a loopback device.
  • The separate endless input_selected loop some reporters mention, which needs an m2m/ISP device.
  • The Rust and Docker lanes of the local gate.

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

@rdoupe
rdoupe requested a review from zackees as a code owner October 11, 2026 05:47
rdoupe and others added 2 commits October 10, 2026 22:56
Selecting a video format in the V4L2 source's properties opens the device
and asks for the capabilities of the selected input. When that query
fails, format_selected returned without closing the device. Devices with
no such input, like the Raspberry Pi's ISP and codec nodes, fail it every
time, and libv4l2 tracks at most 16 open devices per process: after a
few properties refreshes every open fails with "libv4l2: error attempting
to open more than 16 video devices" and no camera works until OBS is
restarted (obsproject/obs-studio#5422, reported by jclab-joseph,
gruzilla, ethagnawl, vicary and mh5).

Measured with the real linux-v4l2 module in a headless libobs on a
loopback device whose selected input does not exist: each opening of the
properties dialog leaves 4 descriptors on the device open, and libv4l2
refuses the device from the 4th dialog on.

Move the open and input capabilities query into
linux-v4l2/v4l2-input-caps.c, with the libv4l2 calls passed in, and test
that a failed query leaves no device open, that repeated failures can
keep opening the device, and the success and open failure paths. The
function keeps today's behaviour, so the failure tests fail.

Refs obs-rust#203.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
format_selected returned without closing the device when the selected
input's capabilities could not be read, so every properties refresh on
such a device left it open until libv4l2's table of 16 devices was full
and no camera could be opened (obsproject/obs-studio#5422, reported by
jclab-joseph, gruzilla, ethagnawl, vicary and mh5).

Close it before returning. The same one-line fix was proposed by
jclab-joseph in the issue and by stephematician in
obsproject/obs-studio#13295.

Fixes obs-rust#203.

Co-Authored-By: stephematician <[email protected]>
Co-Authored-By: jclab-joseph <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Local-Gate: v1 tree=f03beee65a95dc0cf7dadf385e4bf98de45115d2 secs=102 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791698300,"gate":"rust/all/fmt","host":"linux-x86_64","key":"e8d1d4a6d67eaba72d0f2d29da5bc2bc46a76de91bc90b5f2bbcbeb41c2aa9cd","lane":"lint","parents":["90ecd36dcb53564ef39f2f8a8f6bc0409e86bfe4"],"secs":13,"stamp":"e26629139bb2d53b7f41d9aff914cb16","tree":"f03beee65a95dc0cf7dadf385e4bf98de45115d2","v":1,"via":"run"}
Ci-Attestation: {"at":1791698300,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"e8d1d4a6d67eaba72d0f2d29da5bc2bc46a76de91bc90b5f2bbcbeb41c2aa9cd","lane":"lint","parents":["90ecd36dcb53564ef39f2f8a8f6bc0409e86bfe4"],"secs":13,"stamp":"a5681029476a9e639d1b009d70bfbd55","tree":"f03beee65a95dc0cf7dadf385e4bf98de45115d2","v":1,"via":"run"}
Ci-Attestation: {"at":1791698300,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"ae5fed6d117682adf78045cd636eae9ce809141770f354c0aaa238a1751801c1","lane":"rust-test","parents":["90ecd36dcb53564ef39f2f8a8f6bc0409e86bfe4"],"secs":28,"stamp":"449eaaea77d09565cb7291744090c408","tree":"f03beee65a95dc0cf7dadf385e4bf98de45115d2","v":1,"via":"run"}
Ci-Attestation: {"at":1791698300,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"52a877b3a9c0c1526b23659d9da0d6fd50b95944f180671914fab0077cf55946","lane":"tier2-linux","parents":["90ecd36dcb53564ef39f2f8a8f6bc0409e86bfe4"],"secs":61,"stamp":"932db9671f2288e856ab050934e53a1b","tree":"f03beee65a95dc0cf7dadf385e4bf98de45115d2","v":1,"via":"run"}
@zackees

zackees commented Oct 11, 2026

Copy link
Copy Markdown
Member

Verified locally, rebased onto master (which now includes #202) and stamped. The new head is 8e91841dd.

  • RED (935d8bf26): Tier 2 passed 63 of 64. test_v4l2_input_caps failed 2 tests (caps_query_failure_closes_device, repeated_caps_query_failures_keep_opening).
  • GREEN (d7bf57eb2): Tier 2 passed 64 of 64 with ENABLE_RUST_LIBOBS OFF and 64 of 64 with it ON.
  • Rebasing onto linux-v4l2: Keep packed framerate in list values #202 conflicted on the linux-v4l2 source list. I kept both v4l2-framerate.c and v4l2-input-caps.c (gersemi formatted). Local gate: lint, rust-test and tier2-linux all passed, 65 of 65 OFF and ON.

Merging once CI is green.

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: device left open when input caps can't be read, ending in "more than 16 video devices" (upstream #5422)

2 participants