Skip to content

linux-pipewire: Destroy camera devices under the loop lock on unload - #513

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/pipewire-camera-unload-lock
Oct 13, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/pipewire-camera-unload-lock

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 13, 2026

Copy link
Copy Markdown
Collaborator

Description

Quitting OBS with a "Video Capture Device (PipeWire)" source no longer races the PipeWire thread. Before, every quit printed impl_ext_end_proxy called from wrong context, check thread and locking once per camera device, and some quits aborted with double free or corruption (out) or segfaulted while the module unloaded.

  • camera_portal_unload() frees the camera portal's connection. pw_portal_connection_free() (plugins/linux-pipewire/camera-portal.c line 82) destroyed the device table, and camera_device_free() destroyed each device's PipeWire proxy (line 230), on the unloading thread and without the PipeWire thread-loop lock.
  • The PipeWire thread is still running at that point: obs_pipewire_destroy() stops it on the next line. It handles the same connection (registry events, node info and params, the server's replies to the destroys), so the destroys raced it. PipeWire detects this and prints the warning; Device or resource busy in it means the PipeWire thread was running at that moment.
  • The fix locks the loop around the device table's destruction, through two small helpers, obs_pipewire_lock() and obs_pipewire_unlock(), in pipewire.c. Nothing else changes. obs_pipewire_destroy() already stops the thread before it destroys the registry, so those destroys were safe.
  1. test/cmocka: Add a failing test for the camera portal unload: adds test_pipewire_camera_unload (Linux; private PipeWire daemon through test_pipewire_cursor_hdr.sh, private D-Bus bus, X display for obs_startup(); skips without them). Like test_pipewire_camera_unplug from linux-pipewire: Fix use-after-free in log message #393, the real camera portal code runs against a fake org.freedesktop.portal.Camera, and a video source node in the test plays the camera. The test links with --wrap to follow the camera portal's thread loop (lock depth per thread, running or stopped) and checks every pw_proxy_destroy() made by camera-portal.c or pipewire.c while that loop runs: it must be on the loop thread or under the loop lock. The crash itself is rare, but this makes the unsafe destroy deterministic to observe. 1 of 2 cases fails: 3 of 3 proxies destroyed while the loop ran had no lock. The guard (the camera is listed) passes.
  2. linux-pipewire: Destroy camera devices under the loop lock on unload: 2 of 2 pass (3 destroyed while the loop ran, 0 without the lock), and the warning is gone from this test and from test_pipewire_camera_unplug.

Motivation and Context

Fixes #512.

This is a follow-up of our #393. Its "Not fixed here" section named this cause: "On module unload the device proxies are destroyed without the loop lock; PipeWire warns impl_ext_end_proxy called from wrong context at the end of the test, before and after the fix." Our release check then hit the crash once in 15 quits with camera sources (SIGABRT, double free or corruption (out), right after those warnings). The code is upstream's camera portal and unchanged here apart from #393. Stock OBS 32.2.2 prints the same warnings on quit, so this isn't a regression; we're finishing the job #393 left.

Why this one: anyone with a PipeWire camera source gets the warning on every quit, and some quits crash. In the AppImage a crash within 120 s of start also makes AppRun relaunch OBS under gdb. The fix is a few lines, and the cause was already known.

How Has This Been Tested?

Reproduced in the current release AppImage (OBS 33.0.0-beta6-799-gd711a972c, Rust libobs), headless: private Xvfb, a private D-Bus session with a fake camera portal, and a private PipeWire daemon (WirePlumber, no device monitors) with 8 producers posing as 640x480 NV12 cameras. The scene collection has 8 PipeWire camera sources. OBS starts, all 8 cameras negotiate, then OBS gets SIGTERM:

Build Quits Crashed Wrong-context warnings
release, plain quit 110 0 10 per quit, 99 of 110 quits with Device or resource busy
release, quit right after reading and setting every camera's format over obs-websocket, while a test producer comes and goes 80 7 (5 SIGABRT: double free or corruption (out), corrupted size vs. prev_size while consolidating; 2 SIGSEGV) 10-11 per quit
same AppImage with master's linux-pipewire.so (this PR's RED commit) 40 2 (1 SIGABRT double free or corruption (out), 1 SIGSEGV) 10-11 per quit
same AppImage with this PR's linux-pipewire.so 80 0 0

Each crash came right after the warnings, during module unload. The plugins were built from this branch (C lane, RelWithDebInfo) and loaded with the AppImage's own libobs and libpipewire (checked in /proc/<pid>/maps).

Automated, in both libobs lanes (ENABLE_RUST_LIBOBS OFF and ON), on current master:

  • test_pipewire_camera_unload: 1 of 2 cases fails at the RED commit and 2 of 2 pass at GREEN, in each lane. ctest -V shows both cases run, none skipped. 20 of 20 repeated runs pass at GREEN, together with test_pipewire_camera_unplug.
  • Without a display both cases skip; without pipewire the test exits 77 (skipped), like the other PipeWire end-to-end tests.
  • Full ctest at GREEN: 147 of 147 in each lane, under a private Xvfb with software GL and the system libobs-opengl masked.
  • clang-format and gersemi are clean.

Not tested:

  • A physical camera. The test and the app runs use a real PipeWire daemon and the camera portal's own code; only the portal, which hands over the PipeWire connection, is faked.
  • CI has no pipewire binary, so the new test builds and skips there, like test_pipewire_camera_unplug.

Not fixed here (separate causes, also listed in #393): pipewire_camera_destroy() and access_camera() change connection->sources without the loop lock while the registry callbacks iterate it, and obsproject/obs-studio#13971 (a startup race).

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, the test, the 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 13, 2026 08:48
rdoupe and others added 2 commits October 13, 2026 01:54
Quitting OBS with a "Video Capture Device (PipeWire)" source prints
"impl_ext_end_proxy called from wrong context" once per camera device,
and it occasionally aborts (double free). camera_portal_unload() destroys
the camera portal's device proxies from the unloading thread without the
PipeWire thread-loop lock, while the PipeWire thread still runs on the
same connection.

test_pipewire_camera_unload runs the real camera portal against a fake
org.freedesktop.portal.Camera on a private D-Bus bus and a camera node
on a private PipeWire daemon (test_pipewire_cursor_hdr.sh), like
test_pipewire_camera_unplug. It links with --wrap to follow the camera
portal's thread loop (lock depth per thread, stopped or not) and checks
every pw_proxy_destroy() from camera-portal.c and pipewire.c: while the
loop runs, it must be on the loop thread or under the loop lock. The race
itself is rare; this makes the unsafe destroy deterministic to observe.

RED: unload_destroys_device_proxies_under_loop_lock fails, 3 of 3
proxies destroyed while the loop ran had no lock (1 of 2 cases).

Refs obs-rust#512.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
camera_portal_unload() freed the camera portal's device table, and with
it every device's PipeWire proxy, from the unloading thread without the
PipeWire thread-loop lock. The PipeWire thread is still running at that
point (obs_pipewire_destroy() stops it afterwards) and handles the same
connection, so each pw_proxy_destroy() raced it: PipeWire printed
"impl_ext_end_proxy called from wrong context" once per camera device on
every quit, and OBS occasionally aborted with a double free.

Lock the loop around the device table's destruction, through new
obs_pipewire_lock()/obs_pipewire_unlock() helpers.

GREEN: test_pipewire_camera_unload 2 of 2 (3 proxies destroyed while
the loop ran, 0 without the lock); no wrong-context warnings.

Fixes obs-rust#512.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=0d188742342ed45510c96b7c1e6d33e81fb20503 secs=225 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791881922,"gate":"rust/all/fmt","host":"linux-x86_64","key":"7e91aeea664752d737a840565103e2803b3e26e437ce1a0151c83a53e0c694af","lane":"lint","parents":["2f9e6b5b547720e848a2a48e910e77b7c8939d63"],"secs":26,"stamp":"ea94890796f4066e68bf72e29eea82aa","tree":"0d188742342ed45510c96b7c1e6d33e81fb20503","v":1,"via":"run"}
Ci-Attestation: {"at":1791881922,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"7e91aeea664752d737a840565103e2803b3e26e437ce1a0151c83a53e0c694af","lane":"lint","parents":["2f9e6b5b547720e848a2a48e910e77b7c8939d63"],"secs":26,"stamp":"c33f111c25d3dfeb07ad415a04a01bcf","tree":"0d188742342ed45510c96b7c1e6d33e81fb20503","v":1,"via":"run"}
Ci-Attestation: {"at":1791881922,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"61cf00d70bf7ad98e5d5be524c8061a9a2717d56907c1a7ae3fd1afe8f9b381c","lane":"rust-test","parents":["2f9e6b5b547720e848a2a48e910e77b7c8939d63"],"secs":115,"stamp":"3bef2a268bf425044a9a52ed7e9bd348","tree":"0d188742342ed45510c96b7c1e6d33e81fb20503","v":1,"via":"run"}
Ci-Attestation: {"at":1791881922,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"84c6c03787d17e7f3bd3ad40dad7fe449cd6603826e8577e470645a8075c7083","lane":"tier2-linux","parents":["2f9e6b5b547720e848a2a48e910e77b7c8939d63"],"secs":83,"stamp":"27419812da52d553c24c3aa4e839844b","tree":"0d188742342ed45510c96b7c1e6d33e81fb20503","v":1,"via":"run"}
@zackees

zackees commented Oct 13, 2026

Copy link
Copy Markdown
Member

Verified locally (Tier 2 image + pipewire, wireplumber, dbus, under xvfb-run), 2 runs per commit:

  • RED 91be150: test_pipewire_camera_unload fails unload_destroys_device_proxies_under_loop_lock
  • GREEN b873e18: 2/2. It needs PipeWire and a display, so plain CI skips it.
  • Rebased on master, local gate passed; pushed as 5a8b70c.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

2 participants