Repository navigation
linux-pipewire: Destroy camera devices under the loop lock on unload - #513
Merged
Merged
Conversation
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"}
Member
|
Verified locally (Tier 2 image + pipewire, wireplumber, dbus, under xvfb-run), 2 runs per commit: |
zackees
force-pushed
the
fix/pipewire-camera-unload-lock
branch
from
October 13, 2026 08:58
b873e18 to
5a8b70c
Compare
This was referenced Oct 13, 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
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 lockingonce per camera device, and some quits aborted withdouble 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.cline 82) destroyed the device table, andcamera_device_free()destroyed each device's PipeWire proxy (line 230), on the unloading thread and without the PipeWire thread-loop lock.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 busyin it means the PipeWire thread was running at that moment.obs_pipewire_lock()andobs_pipewire_unlock(), inpipewire.c. Nothing else changes.obs_pipewire_destroy()already stops the thread before it destroys the registry, so those destroys were safe.test/cmocka: Add a failing test for the camera portal unload: addstest_pipewire_camera_unload(Linux; private PipeWire daemon throughtest_pipewire_cursor_hdr.sh, private D-Bus bus, X display forobs_startup(); skips without them). Liketest_pipewire_camera_unplugfrom linux-pipewire: Fix use-after-free in log message #393, the real camera portal code runs against a fakeorg.freedesktop.portal.Camera, and a video source node in the test plays the camera. The test links with--wrapto follow the camera portal's thread loop (lock depth per thread, running or stopped) and checks everypw_proxy_destroy()made bycamera-portal.corpipewire.cwhile 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.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 fromtest_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 contextat 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:
Device or resource busydouble free or corruption (out),corrupted size vs. prev_size while consolidating; 2 SIGSEGV)linux-pipewire.so(this PR's RED commit)double free or corruption (out), 1 SIGSEGV)linux-pipewire.soEach 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_LIBOBSOFF 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 -Vshows both cases run, none skipped. 20 of 20 repeated runs pass at GREEN, together withtest_pipewire_camera_unplug.pipewirethe test exits 77 (skipped), like the other PipeWire end-to-end tests.libobs-openglmasked.Not tested:
pipewirebinary, so the new test builds and skips there, liketest_pipewire_camera_unplug.Not fixed here (separate causes, also listed in #393):
pipewire_camera_destroy()andaccess_camera()changeconnection->sourceswithout the loop lock while the registry callbacks iterate it, and obsproject/obs-studio#13971 (a startup race).Types of changes
Checklist:
🤖 Generated with Claude Code