Repository navigation
Description
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 callsclose_session()(which frees and clearssession_handle), theng_cancellable_cancel(), thenbfree(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 usecapturewithout any lock or reference.start()passescapture->session_handletog_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_handleafterclose_session()cleared it, which gives the reported crash ing_variant_new()fromstart(); - uses
captureafterbfree(); - 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 afterclose_session(). It crashes every time withg_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.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
rdoupe commentedon Oct 11, 2026
Claiming: this issue.
test_screencast_portal_teardown, a fake portal on a private D-Bus replying while the capture is destroyed (crashes today).PR within the hour.
linux-pipewire: Drop portal replies for a capture being destroyed
test/cmocka: Add RED test for a portal reply during screencast destroy
rdoupe commentedon Oct 11, 2026
@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 yourg_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-gitin 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.