Skip to content

libobs: obs_shutdown() frees a source's control block while a scene's deferred destroy still releases it (use-after-free, regression from #3431) #3441

Closed

Description

@vpacher

What happens

Under ASan, test_source_profiler fails at obs_shutdown() with a heap-use-after-free:

  • WRITE in obs_source_release() → obs_ref_release() → os_atomic_dec_long(), on the destruction task thread, from obs_source_destroy_defer() → scene_destroy() → remove_all_items() → obs_sceneitem_destroy().
  • The 56-byte block (a source's control block, allocated in obs_source_init()) was freed on the main thread by obs_source_destroy_left_alive() (libobs/obs.c.h), from obs_free_data().

On a local asan build of master (3f39ef6), 4 pinned cores with load, through the sanitizer harness: 29 of 30 runs fail, 26 with the use-after-free. The nightly of 2026-11-01 did not include #3431 yet.

Cause

#3431 (#3364) made obs_free_data() give back the control-block reference of each source the host left alive, right after obs_source_destroy(). But obs_source_destroy() only queues the destroy on the destruction task thread, and other deferred destroys can still hold the source:

  1. obs_free_data() releases the main canvas. The canvas held the scene (output channel 0), so the scene's destroy is queued.
  2. The scene's two inputs are still in data->sources, because the scene's items hold them. The left-alive loop destroys them and frees their control blocks.
  3. The destruction thread then runs scene_destroy(), releases the items, and decrements the freed control blocks.

Before #3431 the control block leaked, so step 3 was harmless.

Fix

  • In obs_free_data(), drain the destruction queue after the canvas and list frees, before the left-alive loop. The scene's teardown then releases its items through the normal last-release path, and only sources the host really leaked reach the loop.
  • For those sources, hold one extra weak reference on each control block while they are destroyed, and give the references back after the final queue wait. A deferred destroy that drops a source's last strong reference then cannot free the block under the loop, and no block is released twice.

The two #3431 cases (test_shutdown_canvas_weak_ref) must still pass.

Not in this fix

Two older hazards in the same code, independent of #3431:

  • obs_free_data() iterates the source tables without sources_mutex while a deferred destroy on the destruction thread can remove entries.
  • When the host leaks both a scene and one of its items, the item's deferred destroy can free the source before the scene's teardown releases it, depending on the hash order.

Activity

  1. added
    bug
    on Nov 1, 2026
  2. vpacher commented on Nov 1, 2026

    @vpacher
    MemberAuthor

    Claiming: #3441

    1. RED: a case in test_shutdown_canvas_weak_ref that forces the order (scene in the main canvas, its inputs released by the scene's deferred destroy) and checks that shutdown leaves no source behind.
    2. GREEN: drain the destruction queue before the left-alive loop; keep a weak reference on each leftover control block until after the final wait.

    First PR: tonight.

  3. added
    ready
    on Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugready

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions