Skip to content

linux-pipewire: Drop portal replies for a capture being destroyed - #247

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/246-pipewire-teardown-race
Oct 11, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/246-pipewire-teardown-race

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator

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 clears session_handle in close_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 NULL session_handle to g_variant_new(), which is the reported crash.
  • The reply uses the capture after it was freed.
  • portal.c frees the same request twice: once in on_cancelled_cb() and once after the response callback.
  1. test/cmocka: Add RED test for a portal reply during screencast destroy adds test_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 when start() begins, and holds the destruction thread just after close_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.
  2. 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.
  • If the portal never replies, the requests stay open until the main loop cancels them, and that sends Request.Close, as before.
  • If the main loop stops running, unload frees the pending captures.
  • The reference held during a reply can be the last one. Releasing it queues 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.

  • C lane: test_screencast_portal_teardown fails 10 of 10 at RED, with the reported g_variant_is_object_path: assertion 'string != NULL' failed followed by SIGSEGV. It passes 10 of 10 at GREEN. Full ctest at GREEN: 83 of 83.
  • Rust lane (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.
  • Natural race, with no holding: a standalone harness loads the real linux-pipewire module and drops the source while the SelectSources reply is handled. Current master crashed in 5 of 40 runs, and GREEN in 0 of 40.
  • CI: the test needs an X display (xvfb-run is enough) and dbus-daemon on PATH. It needs no session bus, dbus-run-session, PipeWire or GL. Without a display or dbus-daemon it exits 77 and ctest reports it as skipped instead of passed. Under xvfb-run ctest it passes, also inside dbus-run-session. The Tier 2 Linux image (rust/tools/linux-validate/Dockerfile) has neither Xvfb nor dbus-daemon and runs ctest without a display, so this test is skipped there, like the other display tests.
  • clang-format and gersemi are clean.

Not tested:

  • Real portals (GNOME, KDE, wlroots/Hyprland) and a real desktop session. The fake portal follows the ScreenCast API, not a particular implementation.
  • The reporter's wedged-portal timeline itself (a stale reply after a 25 s D-Bus timeout). The test covers the race it ends in.
  • Flatpak.

Not changed: camera-portal.c does 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() changes connection->sources without the PipeWire loop lock while registry callbacks iterate that list. That can be fixed separately.

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, commit messages and this text under the submitter's direction, and ran the tests above on the submitter's machine.

🤖 Generated with Claude Code

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]>
@rdoupe
rdoupe requested a review from zackees as a code owner October 11, 2026 16:17
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
zackees force-pushed the fix/246-pipewire-teardown-race branch from 089c2e6 to b7a19ba Compare October 11, 2026 16:28
@zackees

zackees commented Oct 11, 2026

Copy link
Copy Markdown
Member

Verified locally, rebased onto master and stamped. The test needs an X display and dbus-daemon, and Tier 2 has neither, so there it's reported as skipped. I ran it with xvfb-run ctest plus dbus on each commit's Tier 2 build:

  • RED (4802fc942): failed with a portal reply handled during destroy crashed with signal 11.
  • GREEN (089c2e668): passed with --repeat until-fail:5 (5 of 5). Plain Tier 2 passed 82 of 82 with ENABLE_RUST_LIBOBS OFF and 82 of 82 with it ON, and the local gate passed.

Merging once CI is green.

@zackees
zackees merged commit 941217c into obs-rust:master Oct 11, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug: linux-pipewire: screen capture crashes when a portal reply arrives while the source is being destroyed (upstream #13809)

2 participants