Skip to content

linux-v4l2: recover when the camera is held by another process (EBUSY): name the holder, offer to close it, auto-retry #799

Closed

Description

@zackees

Problem

When another process already has a V4L2 camera open, the v4l2_input source fails on start and then stays dead. The UI shows a blank source with no explanation, and the user only gets the camera back by finding and closing the other program themselves.

Seen on NixOS 26.05 (KDE Plasma 6, Wayland) with OBS-Studio-Rust 0.1.0-beta.20261014. A stock OBS 32.1.2 instance left running from earlier held /dev/video0 (Elgato Cam Link 4K). The Rust build's log:

Warning: OBS is already running!
v4l2-input: Start capture from /dev/video0
v4l2-input: Unable to set input 0
v4l2-input: Initialization failed, errno: Device or resource busy

After the other instance was closed with SIGTERM, the same profile captured normally (Stopped capture after 476 frames). The camera and capture path are fine. What's missing is recovery when the device is held by someone else.

Wanted behaviour: break the lock

V4L2 has no lock file. The "lock" is another process holding the device fd open with streaming started. When VIDIOC_S_INPUT / VIDIOC_STREAMON (or open) fails with EBUSY, the source should:

  1. Find the holder. Scan /proc/*/fd for a link to the device node (same uid only) and get pid, comm and exe.
  2. Tell the user. Set a visible source status / properties warning: "Camera in use by obs (pid 1234)". Today it only goes to the log.
  3. Break it. Offer "Close the other program and retry", which sends SIGTERM to the holder, waits for it to release the device, and then retries capture. Don't escalate to SIGKILL silently. Only signal processes owned by the same user.
    • Special case: if the holder is another OBS / OBS-Studio-Rust instance (the "OBS is already running" warning is already detected at startup), make that the prominent choice, because it's the common case.
  4. Retry automatically. While a source is EBUSY, poll every few seconds (or use inotify on the node) and start capture as soon as the device is free, instead of staying dead until the user toggles the source.

Steps 1, 2 and 4 are safe by default. Step 3 needs an explicit user action (or an opt-in setting), because it terminates another program.

Tests (per testing-policy)

  • Tier 1: holder lookup from a fake /proc tree. EBUSY → retry state machine.
  • Black-box: open a v4l2loopback device from a helper process, start OBS with a v4l2_input on it, and assert:
    • the busy status names the helper's pid,
    • capture starts on its own after the helper exits,
    • with "break lock" invoked, the helper gets SIGTERM and capture starts.
  • Differential: stock C reports EBUSY the same way (log line). The new recovery is additive.

Activity

  1. added
    enhancement
    on Oct 15, 2026
  2. zackees commented on Oct 15, 2026

    @zackees
    MemberAuthor

    UX requirement: popup with a "Force Camera" button

    Breaking the lock must go through a modal popup, not only a status line or a properties warning. When a v4l2_input source hits EBUSY, show a dialog:

    Camera is in use
    /dev/video0 (Cam Link 4K) is being used by obs (pid 1234). OBS can't show this camera until that program releases it. Forcing the camera will close that program.

    [ Force Camera ] [ Cancel ]

    • Force Camera: SIGTERM the holder (same uid only), wait for the device to be released, and retry capture. If the holder doesn't exit within a timeout, say so in the dialog. Don't escalate to SIGKILL silently.
    • Cancel: leave the source in the busy state. The automatic retry (step 4 above) still starts capture as soon as the device is freed.
    • Show the dialog once per busy episode, not on every retry poll.
    • Test: the black-box test drives the dialog (or its frontend API equivalent) and asserts that "Force Camera" terminates the helper and capture starts.

    This replaces the "offer" wording in step 3.

  3. zackees commented on Oct 15, 2026

    @zackees
    MemberAuthor

    Claiming: #799 (linux-v4l2 busy-camera recovery).

    1. RED: cmocka test test_v4l2_busy for a new plain-C unit plugins/linux-v4l2/v4l2-busy.c (holder lookup in a /proc tree, a real helper process holding a device node, same-uid-only SIGTERM, retry decision), against stubs.
    2. GREEN: implement v4l2-busy.c and wire it into v4l2-input.c: on EBUSY, name the holder in the source properties, add a Force Camera button (SIGTERM, same uid, no silent SIGKILL), and retry automatically until the device is free.
      Draft PR today.
  4. added
    claimed
    on Oct 15, 2026
  5. zackees commented on Oct 15, 2026

    @zackees
    MemberAuthor

    #800 implements items 1-4: it names the holder, adds a Force Camera button (SIGTERM, same user only, never SIGKILL), retries automatically, and adds tests that use a real helper process holding a device node. Still open here: the modal "Camera is in use" popup, which needs a frontend hook.

  6. removed
    claimed
    on Oct 15, 2026
  7. zackees commented on Oct 15, 2026

    @zackees
    MemberAuthor

    Fixed by #800 (merged): the source names the holder (properties warning + log), a Camera is in use popup with Force Camera (SIGTERM, same user only, never SIGKILL; also a button in the source properties), and automatic retry once a second / 2-30 s backoff. Tests: test_v4l2_busy and test_v4l2_busy_signal (real forked helper processes holding the device). Nothing left open.

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

    enhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions