Repository navigation
linux-v4l2: Keep the virtual camera fd out of child processes - #2285
Conversation
3a0995d to
7b9015f
Compare
zackees
left a comment
There was a problem hiding this comment.
Coordinator review: correct and minimal. All three open() sites gain O_CLOEXEC (vcam try_connect, loopback probe, VA-API render node), matching #340's input-side fix. RED commit precedes GREEN; both new cmocka tests are wired via tests.d, the device-free case runs in CI without vivid. Features ledger line present; the pipe.rs comment correction is accurate. No public header or existing C test touched. Approve once the pending checks finish green.
zackees
left a comment
There was a problem hiding this comment.
Coordinator review (comment; same account as the author, so no approval possible): correct and minimal. All three open() sites gain O_CLOEXEC (vcam try_connect, loopback probe, VA-API render node), matching #340's input-side fix. RED precedes GREEN; both new cmocka tests are wired via tests.d, and the device-free case runs without vivid. Ledger line present; the pipe.rs comment fix is accurate. No public header or existing C test touched. LGTM once pending checks are green.
A Virtual Camera cannot be restarted while OBS records (obsproject/obs-studio#14054). try_connect() in v4l2-output.c opens the v4l2loopback device without O_CLOEXEC, and the recording starts obs-ffmpeg-mux with os_process_pipe_create(), which closes only its own pipe descriptors in the child. The muxer keeps a copy of the device for the whole recording, so stopping the Virtual Camera does not release it and v4l2loopback with exclusive_caps=1 refuses the next start. vaapi_open_display_drm() leaks the encoder's render node the same way. test_v4l2_vcam_cloexec runs the real v4l2-output.c with ioctl() wrapped to read the FD_CLOEXEC flag of the device at VIDIOC_QUERYCAP: - opened_device_is_close_on_exec: try_connect() on a temporary file, so no device, display or module is needed; - virtualcam_restarts_while_a_child_runs: the Virtual Camera on vivid's video output node (node_types bit 8), a child started with os_process_pipe_create() while it runs, then stop and restart while the child lives. Skips without vivid or a display. test_vaapi_open_cloexec runs the real vaapi-utils.c with vaGetDisplayDRM() wrapped: vaapi_open_device() on /dev/null (no GPU), the encoder probe on the first render node, and a child started while the encoder's device is open. The render node cases skip without one. On master all five cases fail. Refs obs-rust#2278
The Virtual Camera could not be restarted while OBS recorded (obsproject/obs-studio#14054). try_connect() opened the v4l2loopback device without O_CLOEXEC, and the recording starts obs-ffmpeg-mux with os_process_pipe_create(), which closes only its own pipe descriptors in the child. The muxer kept a copy of the device until the recording ended: stopping the Virtual Camera closed only OBS's copy, and v4l2loopback with exclusive_caps=1 refused the next start because the device still had a producer. obs-rust#340 fixed the same leak for the V4L2 input. Open the device with O_CLOEXEC in try_connect() and in the loopback_module_available() probe (the non-Linux path). vaapi_open_display_drm() leaked the VA-API encoder's render node into the muxer the same way; it opens with O_CLOEXEC too. The Rust pipe port's comment said Command closes every non-stdio fd in the child. It does not: like posix_spawn in the C, it leaves every inherited fd without FD_CLOEXEC open. The comment says so now. With the fix, on both ENABLE_RUST_LIBOBS lanes (build-c, and the hybrid build-rust): test_v4l2_vcam_cloexec passes 2 of 2. On vivid's output node, a child started while the camera runs holds no copy, the device is closed everywhere after stop, and it is open once after a restart while that child lives. test_vaapi_open_cloexec passes 3 of 3 on renderD128. Before the fix, all 5 cases failed. Fixes obs-rust#2278
7b9015f to
04e3aaf
Compare
Fixes #2278
What and why
On Linux, the Virtual Camera could not be restarted while OBS was recording
(upstream obsproject/obs-studio#14054).
try_connect()inplugins/linux-v4l2/v4l2-output.c.hopened thev4l2loopback device without
O_CLOEXEC. A recording starts obs-ffmpeg-muxwith
os_process_pipe_create(), and that call closes only its own pipe fdsin the child. So the muxer got a copy of the device fd and kept it until
the recording ended. Stopping the Virtual Camera closed only OBS's copy.
With
exclusive_caps=1, v4l2loopback then refused the next start becausethe device still had a producer.
#340 fixed the same leak on the V4L2 input side. This PR does the same for
the output:
try_connect()and theloopback_module_available()probe (the non-Linuxpath) open the device with
O_CLOEXEC.vaapi_open_display_drm()inplugins/obs-ffmpeg/vaapi-utils.c.hleakedthe VA-API encoder's render node into the muxer the same way. It now opens
with
O_CLOEXECtoo.rust/obs-util/src/platform/unix/pipe.rs: the comment saidCommandcloses every non-stdio fd in the child. It does not. Like the C
posix_spawn, it leaves open every inherited fd that lacksFD_CLOEXEC.The comment now says so.
New tests (in
tests.d, like #340'stest_v4l2_fd_cloexec):test_v4l2_vcam_cloexecruns the real v4l2-output.c withioctl()wrapped. The wrapper reads the device fd's
FD_CLOEXECflag atVIDIOC_QUERYCAP.opened_device_is_close_on_execcallstry_connect()on a temporaryfile. It needs no device, display or kernel module, so it runs in CI.
virtualcam_restarts_while_a_child_runsstarts the Virtual Camera onvivid's video output node, starts a child with
os_process_pipe_create(), then stops and restarts the camera while thechild lives. It reads
/proc/<pid>/fdfor the child and for OBS.modprobe vivid node_types=0x100, as root) or without core video.VIDIOC_S_PARMfor its output (v4l2loopback has one), sothe wrapper answers that single ioctl for the vivid node.
virtualcam_start()is not called, because without v4l2loopback itruns
pkexec modprobe.test_vaapi_open_cloexecruns the real vaapi-utils.c withvaGetDisplayDRM()wrapped. Its cases:vaapi_open_device()on/dev/null(no GPU needed);The two render-node cases skip without a render node.
RED → GREEN
Both lanes use the build4 tree: build-c, and build-rust (hybrid, with
OBS_BUILD_C_REFERENCE=ON). Tests ran under a private Xvfb and D-Bus, witha fresh HOME. vivid was loaded with
node_types=0x100, and renderD128 waspresent.
runs were made). On both lanes all 5 cases fail:
opened_device_is_close_on_exec: "try_connect() opened the devicewithout O_CLOEXEC".
virtualcam_restarts_while_a_child_runs: copies of the vivid node were1 in a child while running, 1 after stop, 2 after restart, and 1 in a
child started after the restart.
device_is_opened_close_on_execandprobe_opens_render_nodes_close_on_exec: "1 of 1 device opens lackO_CLOEXEC".
encoder_device_is_not_inherited: the child holds 1 copy ofrenderD128.
test_v4l2_vcam_cloexecpasses 2/2. The copies are 0 while running, 0after stop, 1 after restart (OBS's own), and 0 in the second child.
test_vaapi_open_cloexecpasses 3/3.FD_CLOEXECcase still passes. The exit code is 0.--test-diff origin/master(Rust workspace3904/3904, 6 skipped),
--lane lintand--lane crossall ok.--lane lintwas run again on 7b9015f and is ok. libobs is unchanged, so theexported symbols are unchanged.
not have. The vivid case drives the real output code through the same
start, stop and restart sequence.
Checklist
masterdocs/features.mdis added (Fixed upstream bugs, Video capture)Note for reviewers
After the final rebase, the vivid case of
test_v4l2_vcam_cloexecfailed once on the hybrid build, in the first run after a rebuild. Its error message was not captured. 135 later runs passed, including under full CPU load and with vivid reloaded before each run. The CI case (temp file, no device) is unaffected.