Skip to content

fix(canvas): don't read a scene after releasing the canvas's last ref to it (#2049) - #2052

Merged
zackees merged 4 commits into
masterfrom
fix/2049-canvas-teardown
Oct 24, 2026
Merged

zackees merged 4 commits into
masterfrom
fix/2049-canvas-teardown

Conversation

@zackees

@zackees zackees commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

Fixes #2049

Cause

obs_canvas_destroy() walks its source list and releases the reference a SCENE_REF canvas holds on each scene. It read source->context.hh.next after obs_source_release(source). When that was the last reference, obs_source_destroy() queued the scene's free on obs->destruction_task_thread (obs_source_destroy_defer). The walk's read then raced that free on another thread. It loses only rarely, which is why test_context_names (test_canvas_sources, during teardown) crashed only now and then.

Fix

Take hh.next before the release. A destroyed scene stays in the list: with the canvas at zero references, obs_canvas_remove_source() cannot find the canvas to unlink it. So the saved next pointer stays valid. Releasing one scene never frees another one in this canvas, because each scene still holds the canvas's own reference until its turn. The HASH_CLEAR that #2041 added frees only the table.

Test, and why RED is not deterministic

test/cmocka/test_canvas_destroy_scene_refs.c (public API, Linux, under xvfb like test_shutdown_canvas_weak_ref):

  • builds 100 private SCENE_REF | EPHEMERAL canvases, each holding the last reference to 16 scenes;
  • destroys each canvas, which runs the same walk;
  • sets mallopt(M_PERTURB), so a lost race follows a garbage pointer.

Because the free runs on the destruction thread, no public API makes the race deterministic. The first RED commit (4 scenes) passed in CI run 40063990812. The recorded failure is #2049's crash in run 40061416995: test_context_names → test_canvas_sources → Segmentation fault(11)Test teardown failed. The stress test now runs the race 1,600 times per run, so a regression is more likely to show up, but it is not guaranteed to.

Tiers: Tier 2 (libobs, the hybrid and libobs-c-reference). No Rust or public header change.

@zackees

zackees commented Oct 24, 2026

Copy link
Copy Markdown
Member Author

The hh.next fix alone did not stop the crash (run 40065512336 still segfaulted in test_context_names teardown). Second use after free in the same function: HASH_CLEAR (added in #2041) reads head->hh.tbl, and the head is often the scene the loop just released, which the destruction task thread may already have freed. Pushed a fix that clears the list before releasing anything. The local Tier 2 lane does not build here, so CI is the check.

@zackees
zackees force-pushed the fix/2049-canvas-teardown branch from 35164f4 to a8fe884 Compare October 24, 2026 05:40
@zackees
zackees marked this pull request as ready for review October 24, 2026 05:50
@zackees
zackees added this pull request to the merge queue Oct 24, 2026
Merged via the queue into master with commit a35c779 Oct 24, 2026
31 checks passed
@zackees
zackees deleted the fix/2049-canvas-teardown branch October 26, 2026 15:43
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: test_context_names segfaults intermittently in test_canvas_sources (Tier 2 Linux, Rust ON)

1 participant