Skip to content

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

Closed

Description

@rdoupe

Upstream OBS bug, reported by mattpotts85 in obsproject/obs-studio#13809. Code unchanged in this fork, including after the PipeWire reconnect work in #109.

Location

  • plugins/linux-pipewire/screencast-portal.c: screencast_portal_capture_destroy() runs on libobs's destruction thread. It calls close_session() (which frees and clears session_handle), then g_cancellable_cancel(), then bfree(capture).
  • The portal replies (on_create_session_response_received_cb, on_select_source_response_received_cb, on_start_response_received_cb, on_pipewire_remote_opened_cb) run on the main loop and use capture without any lock or reference. start() passes capture->session_handle to g_variant_new("(osa{sv})", ...).
  • plugins/linux-pipewire/portal.c: on_cancelled_cb() frees the request on the cancelling thread. on_response_received_cb() frees the same request after its callback returns.

Class

Crash: NULL dereference, use-after-free and double free (data race between two threads).

How it is reached

The main loop starts handling a portal Response for a screen capture source while the source's last reference is dropped, for example when the source is removed, its scene is deleted, or a scene collection is switched. Cancelling from the destruction thread unsubscribes replies that have not started yet, but it cannot stop one that is already running. That reply then:

  • reads session_handle after close_session() cleared it, which gives the reported crash in g_variant_new() from start();
  • uses capture after bfree();
  • frees its request after on_cancelled_cb() on the other thread already freed it.

A portal that answers late makes this more likely. In the report, a wedged xdg-desktop-portal-hyprland recovered and delivered stale replies while capture sources were being created and destroyed.

Reproduced headless on a private D-Bus bus against a fake ScreenCast portal (no real portal, PipeWire or session bus):

  • Natural race, with the source dropped while the SelectSources reply is handled: current master crashed in 5 of 40 runs (with the fix: 0 of 40).
  • Deterministic: the source is dropped when start() begins, and the destruction thread is held just after close_session(). It crashes every time with g_variant_is_object_path: assertion 'string != NULL' failed, the reported signature.

Proposed fix and failing test

Fix: while the main loop handles a portal reply, it holds a strong reference to the source, taken from the weak reference that the reconnect callbacks already use. Destruction cannot start while that reference is held, and a reply that arrives once destruction has started is dropped. destroy() still stops the stream on the destruction thread. It cancels the portal requests and frees the capture on the main loop, which is the thread that runs the replies, so no reply can see freed memory and the request is freed once. Module unload frees anything the main loop did not free. destroy() never waits.

The NULL check suggested in the report would stop the g_variant_new() crash in the common case. It would still race with close_session(), and it would not fix the use-after-free or the double free.

RED test (cmocka, test_screencast_portal_teardown): runs the real portal code against a fake portal on a private bus. It drops the source when start() begins, and holds the destruction thread just after close_session(). It crashes at RED and passes with the fix.

camera-portal.c does not have this bug: its portal requests belong to one module-wide connection and take no per-source data. It has a separate race in which pipewire_camera_destroy() changes connection->sources without the PipeWire loop lock while registry callbacks iterate it. That is not fixed here.

Activity

  1. rdoupe commented on Oct 11, 2026

    @rdoupe
    CollaboratorAuthor

    Claiming: this issue.

    1. RED: test_screencast_portal_teardown, a fake portal on a private D-Bus replying while the capture is destroyed (crashes today).
    2. GREEN: portal replies hold a reference to the source and are dropped once destruction starts; cancelling requests and freeing the capture move to the main loop.
      PR within the hour.
  2. added a commit that references this issue on Oct 11, 2026
    b7a19ba
  3. rdoupe commented on Oct 11, 2026

    @rdoupe
    CollaboratorAuthor

    @mattpotts85 you reported this in obsproject/obs-studio#13809, so a heads-up: it's fixed here in obs-rust by #247, merged today with a regression test.

    Cause, as your analysis said: a portal reply (SelectSources, Start, ...) could still run after the capture's session was torn down, so start() built a D-Bus call from a NULL session handle, which is your g_variant_builder_add_value (value=0x0) crash. The test also found the wider race behind it: destroy() ran on the destruction thread while portal callbacks ran on the main loop, which could also free the request twice. Now each portal reply holds a reference to the source and is dropped once destruction has started, and cancelling the portal requests and freeing the capture happen on the main loop, the same thread as the replies. With a fake portal on a private D-Bus, the reply-during-destroy case crashed 10 of 10 runs before and passes 10 of 10 after; a 40-run stress went from 5 crashes to 0.

    Workaround on stock OBS: the crash needs a portal reply to arrive after the source is gone, so avoid removing a screen capture source, switching its capture type or closing OBS while the portal's share picker is still open or hanging.

    obs-rust is an AI-assisted fork of OBS; on Arch it's obs-studio-rust-git in the AUR.

    If you get a chance to try it on your Hyprland setup, a short reply here on whether the crashes are gone would really help.

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

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions