Repository navigation
linux-v4l2: Close the device when input caps cannot be read - #204
Merged
Merged
Conversation
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"}
Member
|
Verified locally, rebased onto master (which now includes #202) and stamped. The new head is
Merging once CI is green. |
zackees
force-pushed
the
fix/203-v4l2-caps-fd-leak
branch
from
October 11, 2026 05:58
d7bf57e to
8e91841
Compare
This was referenced Oct 11, 2026
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 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_ENUMINPUTfails, 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 withlibv4l2: error attempting to open more than 16 video devicesand no camera can be opened until OBS restarts. With this change the device is closed on that path.The cause:
format_selected()returned withoutv4l2_close(dev)whenv4l2_get_input_caps()failed (plugins/linux-v4l2/v4l2-input.c:673-674). Every other early return in the plugin closes the device.test/cmocka: Add RED V4L2 input caps device leak tests: moves the open + caps query, unchanged, intov4l2_open_input_caps()inplugins/linux-v4l2/v4l2-input-caps.[ch](open, close and the caps query are injected;format_selecteduses it) and addstest_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"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_capsfails 2 of 4 at the RED commit and passes 4 of 4 at the GREEN commit; thelinux-v4l2plugin builds cleanly; clang-format is clean.By hand, headless, with the real
linux-v4l2.soin 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, soVIDIOC_ENUMINPUTfails. Each iteration opens the properties, fires the pixel-format modified callback and destroys the properties:Not tested:
input_selectedloop some reporters mention, which needs an m2m/ISP device.Types of changes
Checklist:
🤖 Generated with Claude Code