Repository navigation
linux-pipewire: Drop portal replies for a capture being destroyed - #247
Merged
Merged
Conversation
Portal replies are handled on the main loop, but libobs destroys sources on its destruction thread. A SelectSources reply that is being handled while the capture is destroyed runs start() with the session already closed, and crashes in g_variant_new() with a NULL session handle, or uses the capture after it was freed. Run the screencast portal code against a fake portal on a private D-Bus bus that the test starts itself, release the source when start() begins, and hold the destruction thread just after it closed the session. The test needs an X display (xvfb-run is enough) and dbus-daemon, but no session bus, PipeWire or portal; without them ctest reports it skipped. Reported upstream as obsproject/obs-studio#13809 by mattpotts85. Refs obs-rust#246. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Portal replies are handled on the main loop, but libobs destroys sources on its destruction thread. destroy() closed the session and then cancelled the pending portal requests, but cancelling from that thread cannot stop a reply the main loop is already handling, so start() could run with the session closed and crash in g_variant_new(), or a reply could use the capture after it was freed. Hold a strong reference to the source while a reply is handled: the source cannot start being destroyed while it is held, and a reply that comes in once destruction has begun is dropped. destroy() still stops the stream on the destruction thread, but cancels the portal requests and frees the capture on the main loop, so a later reply never sees freed memory. Unloading frees what the main loop has not. Reported upstream as obsproject/obs-studio#13809 by mattpotts85. Fixes obs-rust#246. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Local-Gate: v1 tree=069e004017eba5be8a5c3036d3cbf4034688be10 secs=122 lanes=lint:run,rust-test:run,tier2-linux:run Ci-Attestation: {"at":1791736091,"gate":"rust/all/fmt","host":"linux-x86_64","key":"16cdcb7957063f98962813c9e3123e0b3dd739fd5202d363483c064cdb3e0d72","lane":"lint","parents":["4802fc942373aad6370231839b3e2a5a62348503"],"secs":48,"stamp":"e3a0e90dbc49a79a9cee068a9ad5dcd1","tree":"069e004017eba5be8a5c3036d3cbf4034688be10","v":1,"via":"run"} Ci-Attestation: {"at":1791736091,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"16cdcb7957063f98962813c9e3123e0b3dd739fd5202d363483c064cdb3e0d72","lane":"lint","parents":["4802fc942373aad6370231839b3e2a5a62348503"],"secs":48,"stamp":"b49ef44875747209785c8a329aefa9fa","tree":"069e004017eba5be8a5c3036d3cbf4034688be10","v":1,"via":"run"} Ci-Attestation: {"at":1791736091,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"d784984b1cff1833fbbd8de7ff71947416c10177aafa7bbe268a48d686cd418b","lane":"rust-test","parents":["4802fc942373aad6370231839b3e2a5a62348503"],"secs":29,"stamp":"bd8aed569c393f4f84a6ef0e3ccae16c","tree":"069e004017eba5be8a5c3036d3cbf4034688be10","v":1,"via":"run"} Ci-Attestation: {"at":1791736091,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"1102ce2d6adda43973f500fff7bf50aec87abd790c2d261adfc2ddaf4cdf1972","lane":"tier2-linux","parents":["4802fc942373aad6370231839b3e2a5a62348503"],"secs":45,"stamp":"93215ed7abbeeb1c50fd56bbe2c2282e","tree":"069e004017eba5be8a5c3036d3cbf4034688be10","v":1,"via":"run"}
zackees
force-pushed
the
fix/246-pipewire-teardown-race
branch
from
October 11, 2026 16:28
089c2e6 to
b7a19ba
Compare
Member
|
Verified locally, rebased onto master and stamped. The test needs an X display and
Merging once CI is green. |
zackees
approved these changes
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 screen capture source could crash OBS if a portal reply was handled while the source was being destroyed. With this change the reply is dropped, and the capture is freed only after no reply can reach it.
The cause is a race between two threads. Portal replies run on the main loop, but
screencast_portal_capture_destroy()runs on libobs's destruction thread. That function clearssession_handleinclose_session(), cancels the portal requests and frees the capture. Cancelling from that thread cannot stop a reply that the main loop is already handling. That reply then hits one of three failures:start()passes a NULLsession_handletog_variant_new(), which is the reported crash.portal.cfrees the same request twice: once inon_cancelled_cb()and once after the response callback.test/cmocka: Add RED test for a portal reply during screencast destroyaddstest_screencast_portal_teardown. It runs the real screencast portal code against a fake ScreenCast portal on a private D-Bus bus that the test starts itself. It drops the last reference to the source whenstart()begins, and holds the destruction thread just afterclose_session(). That hold only widens a window that already exists. The scenario runs in a forked child with a 20 s watchdog. It crashes here.linux-pipewire: Drop portal replies for a capture being destroyed. Each portal reply, and each portal call failure that was not cancelled, takes a strong reference to the source from the weak reference that the plugins: Reconnect Linux capture after PipeWire restarts #109 reconnect callbacks already use. Destruction cannot start while the reply holds it, and a reply that arrives after destruction has started is dropped.destroy()still stops the stream and closes the session on the destruction thread. It then hands the capture to the main loop, which cancels the portal requests and frees the capture there. Cancelling on the thread that runs the replies unsubscribes the replies still pending and frees each request once. Module unload frees any capture the main loop has not freed yet. The test passes.Threading:
destroy()never waits for the main loop or the portal, so it adds no hang.Request.Close, as before.destroy()to the destruction thread as usual; the source is never destroyed inline on the main loop.Motivation and Context
Fixes #246 (upstream obsproject/obs-studio#13809, reported by mattpotts85)
Why this one: it is a hard crash with byte-identical coredumps, and four happened in one session. It hits when sources are removed or scenes change while the portal is slow, which is common on wlroots/Hyprland portals under load. The reconnect work in #109 does not cover it: current master still crashes. The NULL check suggested upstream would stop the reported crash in the common case. It would still race with
close_session()on the other thread, and it would leave the use-after-free and the double free.How Has This Been Tested?
Automated and headless. Every run used a private Xvfb, a private D-Bus bus and a fake portal, with a scratch runtime directory. No session bus, real portal or PipeWire daemon was reachable.
test_screencast_portal_teardownfails 10 of 10 at RED, with the reportedg_variant_is_object_path: assertion 'string != NULL' failedfollowed by SIGSEGV. It passes 10 of 10 at GREEN. Full ctest at GREEN: 83 of 83.ENABLE_RUST_LIBOBS=ON): the new test fails 5 of 5 at RED and passes 10 of 10 at GREEN. Full ctest: 83 of 83.linux-pipewiremodule and drops the source while the SelectSources reply is handled. Current master crashed in 5 of 40 runs, and GREEN in 0 of 40.xvfb-runis enough) anddbus-daemononPATH. It needs no session bus,dbus-run-session, PipeWire or GL. Without a display ordbus-daemonit exits 77 and ctest reports it as skipped instead of passed. Underxvfb-run ctestit passes, also insidedbus-run-session. The Tier 2 Linux image (rust/tools/linux-validate/Dockerfile) has neither Xvfb nordbus-daemonand runs ctest without a display, so this test is skipped there, like the other display tests.Not tested:
Not changed:
camera-portal.cdoes not have this bug, because its portal requests belong to one module-wide connection and do not use per-source data. It has a separate race:pipewire_camera_destroy()changesconnection->sourceswithout the PipeWire loop lock while registry callbacks iterate that list. That can be fixed separately.Types of changes
Checklist:
🤖 Generated with Claude Code