Skip to content

fix(libobs): obs_shutdown() frees the control block of a source left alive (#3364) - #3431

Merged
zackees merged 2 commits into
masterfrom
fix/3364-shutdown-control-leak
Nov 1, 2026
Merged

zackees merged 2 commits into
masterfrom
fix/3364-shutdown-control-leak

Conversation

@vpacher

@vpacher vpacher commented Nov 1, 2026

Copy link
Copy Markdown
Member

Fixes #3364. Refs #2038, #2022, #3238.

Why

obs_free_data() destroys every source the host left alive with obs_source_destroy(), but unlike obs_source_release() it never gives back the reference the source holds on its control block. Each such source leaked its 56-byte control block (LeakSanitizer in test_shutdown_canvas_weak_ref and test_shutdown_type_lifetime, at obs_context_init_control). test_shutdown_canvas_weak_ref pinned the leak as by design, because the host's references can outlive the core.

Those references stay safe without the leak:

  • obs_source_release() after obs_shutdown() returns at its "no core" check before it touches the control block.
  • A weak reference the host still holds keeps its own count on the block, so the block stays valid until the host releases it.

What changes

  • obs_free_data() destroys the leftover sources through obs_source_destroy_left_alive(): obs_source_destroy(), then obs_weak_source_release() of the control block, the same two steps as obs_source_release(). The block is freed unless the host holds a weak reference.
  • FREE_OBS_HASH_TABLE_WITH() takes the destroy function, so the log line ("%d source(s) were remaining") stays as it was. FREE_OBS_HASH_TABLE() uses it with obs_<type>_destroy as before.
  • test_shutdown_canvas_weak_ref: the existing case now expects no allocation left after obs_shutdown() (was 2, the leak). A new case holds a weak reference across obs_shutdown(): the control block stays (1 allocation), and releasing the weak reference frees it (0). This changes what an existing fork test expects, on purpose; review by its author is welcome.
  • The two bug entries of bug: libobs: LeakSanitizer reports the context control of scenes alive at obs_shutdown (follow-up to #2038) #3364 leave the sanitizer known list.

Commits

  • RED 572cc4b: the test expectations and the known list. Fails both cases (2 != 0, 1 != 0).
  • GREEN f992357: the fix.

Tests

Both tests through the sanitizer harness (asan build) on a local build box:

  • RED (master obs.c.h): 5 direct leaks, all at obs_context_init_control; test_sources_left_alive_free_their_canvas_ref fails (2 != 0), test_weak_ref_held_across_shutdown_frees_on_release fails (1 != 0).
  • GREEN: no leak; 2/2 cases pass, test_shutdown_type_lifetime passes.

Tier 2 (the tests against libobs and libobs-c-reference) passes in the local gate.

  • Local gate, stamped head f992357: 4 min 28 s (lint 39 s, rust-test 83 s, cross 39 s, tier2-linux 267 s).

Checklist

🤖 Generated with Claude Code

@vpacher
vpacher requested a review from zackees November 1, 2026 12:59
…left alive (#3364)

obs_free_data() destroys every source the host left alive with
obs_source_destroy() but, unlike obs_source_release(), never gives back
the reference the source holds on its control block, so each one
leaked 56 bytes (LeakSanitizer in test_shutdown_canvas_weak_ref and
test_shutdown_type_lifetime, #3364). The leak was pinned as by design.

test_shutdown_canvas_weak_ref now expects nothing left after
obs_shutdown(), and a new case checks that a weak reference the host
still holds keeps the control block alive until the host releases it.
The two known-list entries of #3364 go.

Fails: test_sources_left_alive_free_their_canvas_ref (2 allocations
left, want 0) and test_weak_ref_held_across_shutdown_frees_on_release
(1 left after the release, want 0).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@zackees

zackees commented Nov 1, 2026

Copy link
Copy Markdown
Member

Coordinator: this conflicts with master in rust/tools/sanitize/triage.py (known list), and maintainer edits are off, so I can't rebase it for you. The resolution should be to keep neither side of the hunk:

Rebase onto origin/master with that, then run --auto again or ping me and I'll queue it.

…e left alive (#3364)

obs_free_data() destroys the sources the host left alive through
obs_source_destroy_left_alive(), which then gives back the source's
reference on its control block with obs_weak_source_release(), the same
two steps as obs_source_release(). The block is freed unless the host
still holds a weak reference; that reference keeps it valid, and its
release after obs_shutdown() frees it. obs_source_release() after
obs_shutdown() still returns before it touches the block.

FREE_OBS_HASH_TABLE_WITH() takes the destroy function, so the log line
("%d source(s) were remaining") stays as it was.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=07ddf02f446cc17052f38281d671396b63f588e3 secs=318 lanes=lint:run,rust-test:run,cross:run,tier2-linux:run,macos-native:n/a,windows-native:n/a
Ci-Attestation: {"at":1793540971,"gate":"rust/all/fmt","host":"linux-x86_64","key":"ecc4a9f41a51f81ac03dbd058d445750f88c2d4253d0d9c8c8e23c314ad6891f","lane":"lint","parents":["1a657c8bc175465e6f59b959bd43fe0360a59ae5"],"secs":85,"stamp":"b130b3ad21193cfe43942fa84311e164","tree":"07ddf02f446cc17052f38281d671396b63f588e3","v":1,"via":"run"}
Ci-Attestation: {"at":1793540971,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"ecc4a9f41a51f81ac03dbd058d445750f88c2d4253d0d9c8c8e23c314ad6891f","lane":"lint","parents":["1a657c8bc175465e6f59b959bd43fe0360a59ae5"],"secs":85,"stamp":"3172032c12cbfc3711d92e39052eaf38","tree":"07ddf02f446cc17052f38281d671396b63f588e3","v":1,"via":"run"}
Ci-Attestation: {"at":1793540971,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"19dc957fef602b46d4f8470cd84bb6265dc3e9d8fb24cbd369f07306a4a23624","lane":"rust-test","parents":["1a657c8bc175465e6f59b959bd43fe0360a59ae5"],"secs":122,"stamp":"4d4ac7c21c6c6f22f7a95c7115a4b4df","tree":"07ddf02f446cc17052f38281d671396b63f588e3","v":1,"via":"run"}
Ci-Attestation: {"at":1793540971,"gate":"rust/x86_64-pc-windows-msvc/clippy","host":"linux-x86_64","key":"c369486aed1098cc7d724aa02332d38869a22ce555078ea357e1daab27882e0b","lane":"cross","parents":["1a657c8bc175465e6f59b959bd43fe0360a59ae5"],"secs":111,"stamp":"aab370080e5f39e2c358435d420104e1","tree":"07ddf02f446cc17052f38281d671396b63f588e3","v":1,"via":"run"}
Ci-Attestation: {"at":1793540971,"gate":"rust/x86_64-pc-windows-msvc/dylint","host":"linux-x86_64","key":"c369486aed1098cc7d724aa02332d38869a22ce555078ea357e1daab27882e0b","lane":"cross","parents":["1a657c8bc175465e6f59b959bd43fe0360a59ae5"],"secs":111,"stamp":"803f9173caeb1153a5f38285c0cb7006","tree":"07ddf02f446cc17052f38281d671396b63f588e3","v":1,"via":"run"}
Ci-Attestation: {"at":1793540971,"gate":"rust/aarch64-apple-darwin/clippy","host":"linux-x86_64","key":"c369486aed1098cc7d724aa02332d38869a22ce555078ea357e1daab27882e0b","lane":"cross","parents":["1a657c8bc175465e6f59b959bd43fe0360a59ae5"],"secs":111,"stamp":"c8f8f97cc8c93a9565886968b02244cd","tree":"07ddf02f446cc17052f38281d671396b63f588e3","v":1,"via":"run"}
Ci-Attestation: {"at":1793540971,"gate":"rust/aarch64-apple-darwin/dylint","host":"linux-x86_64","key":"c369486aed1098cc7d724aa02332d38869a22ce555078ea357e1daab27882e0b","lane":"cross","parents":["1a657c8bc175465e6f59b959bd43fe0360a59ae5"],"secs":111,"stamp":"508fb202d3561d0d6db9dd6324dcea0d","tree":"07ddf02f446cc17052f38281d671396b63f588e3","v":1,"via":"run"}
Ci-Attestation: {"at":1793540971,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"1093b41de2262e92f4e5024a3f68cc38bdfb301862de899225b2bcd59045cc8d","lane":"tier2-linux","parents":["1a657c8bc175465e6f59b959bd43fe0360a59ae5"],"secs":282,"stamp":"e42323cc123da3441058c6f2787654e2","tree":"07ddf02f446cc17052f38281d671396b63f588e3","v":1,"via":"run"}
@vpacher
vpacher force-pushed the fix/3364-shutdown-control-leak branch from f992357 to 06aee4d Compare November 1, 2026 13:49
@vpacher

vpacher commented Nov 1, 2026

Copy link
Copy Markdown
Member Author

Rebased onto master with neither side of the hunk kept: the two #3364 entries go with this fix, and the #3362 line was already gone. New head 06aee4d, local gate stamped (5 min 19 s).

Both tests under asan with the fix (sanitizer harness, asan build, local build box): no leak reports, test_shutdown_canvas_weak_ref 2/2 cases pass, test_shutdown_type_lifetime passes. With master's obs.c.h the same build reports 5 direct leaks, all at obs_context_init_control, and both cases of test_shutdown_canvas_weak_ref fail (2 != 0, 1 != 0).

The PR changes what test_shutdown_canvas_weak_ref expects (from 2 allocations left to 0), on purpose; the new case shows that a weak reference the host still holds keeps the control block until it is released. A review of that from you as the test's author would be welcome before it goes in.

@zackees zackees left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coordinator review: thanks for the rebase. The conflict was resolved as suggested and both #3364 known-list entries are gone. The fix is right: obs_source_destroy_left_alive mirrors the control-block release in obs_source_release(), and the new test_weak_ref_held_across_shutdown_frees_on_release covers the case that justified the old by-design leak. The assertion change in test_shutdown_canvas_weak_ref.c is fine under the "never edit existing C tests" rule. That test was added in this repo (#2041), not upstream, and it changes because the behaviour it pinned was deliberately fixed, not to make a port pass.

@zackees
zackees added this pull request to the merge queue Nov 1, 2026
Merged via the queue into master with commit 78f0af1 Nov 1, 2026
41 checks passed
@zackees
zackees deleted the fix/3364-shutdown-control-leak branch November 1, 2026 14:44
alerque pushed a commit to alerque/obs-studio that referenced this pull request Nov 2, 2026
…destroys what is left (obs-rust#3441) (obs-rust#3442)

* fix(libobs): RED: obs_shutdown() lets a scene release its items before it destroys what is left (obs-rust#3441)

obs_free_data() releases the main canvas, which queues the destroy of
the scene it held on the destruction task thread. Since obs-rust#3431 the
left-alive loop frees the control block of each source it destroys, and
the scene's inputs are still in the source table until its deferred
destroy releases its items. When the loop reaches them first, the
scene's teardown then releases freed control blocks: a heap-use-after-
free under ASan (test_source_profiler, 29 of 30 runs on 4 loaded
cores).

The new case holds the destruction thread for 200 ms in the scene's
first item_remove signal, which forces that order, and checks that
shutdown logs no "source(s) were remaining": the scene's inputs are
released by the scene, not destroyed as left alive. Without ASan the
use-after-free itself is invisible, so the log line is the check.

Fails: test_scene_items_are_released_not_left_alive (the left-alive
loop logs "2 source(s) were remaining").

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(libobs): GREEN: obs_shutdown() lets a scene release its items before it destroys what is left (obs-rust#3441)

obs_free_data() now waits until the destruction task queue is empty
before its left-alive loop. The destroy of the scenes the main canvas
held runs first, and their teardown releases their items through
obs_source_release(), which frees each control block in the normal
way. Only the sources the host left alive stay in the tables.

The left-alive loop is plain obs_source_destroy() again. Before it,
obs_free_data() takes a weak reference on the control block of each
source left alive, and gives the references back after the final queue
wait: a deferred destroy the loop starts (a scene the host left alive
releasing its items) can drop a source's last strong reference, and the
hold keeps the block valid until then. If that happened, the source's
own reference is already given back and only the hold goes; otherwise
both go, as obs-rust#3431 did. The final wait also repeats until the queue is
empty, as obs_shutdown() does at its start.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=c6439dc37a0145fa5bc5028137f2ff1b1b6c424e secs=267 lanes=lint:run,rust-test:run,cross:run,tier2-linux:run,macos-native:n/a,windows-native:n/a
Ci-Attestation: {"at":1793562864,"gate":"rust/all/fmt","host":"linux-x86_64","key":"117417cebcc0a1b4fb051550efdabe0640e2f346e85057d8d3a6aa5d45b65f9d","lane":"lint","parents":["a8f30ed6f587707dee2677413a9e0419adc7264d"],"secs":40,"stamp":"e9ae2c5a591359279ea4d59b59c5b9ca","tree":"c6439dc37a0145fa5bc5028137f2ff1b1b6c424e","v":1,"via":"run"}
Ci-Attestation: {"at":1793562864,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"117417cebcc0a1b4fb051550efdabe0640e2f346e85057d8d3a6aa5d45b65f9d","lane":"lint","parents":["a8f30ed6f587707dee2677413a9e0419adc7264d"],"secs":40,"stamp":"daf439d257486fe748af24fb3d71b09d","tree":"c6439dc37a0145fa5bc5028137f2ff1b1b6c424e","v":1,"via":"run"}
Ci-Attestation: {"at":1793562864,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"970d1ef2bb26c86fb8b9fa766981824e53d382bd8227ee69ae94ff8523b7272e","lane":"rust-test","parents":["a8f30ed6f587707dee2677413a9e0419adc7264d"],"secs":82,"stamp":"d97c9400078326df9e93273f14b0a30b","tree":"c6439dc37a0145fa5bc5028137f2ff1b1b6c424e","v":1,"via":"run"}
Ci-Attestation: {"at":1793562864,"gate":"rust/x86_64-pc-windows-msvc/clippy","host":"linux-x86_64","key":"cb88e9fcab2c55994b3ec27de674531ca9553155a446b5924672d4f18e0552e0","lane":"cross","parents":["a8f30ed6f587707dee2677413a9e0419adc7264d"],"secs":38,"stamp":"cb29f82d2eaa8c53c797e8adf0083bd1","tree":"c6439dc37a0145fa5bc5028137f2ff1b1b6c424e","v":1,"via":"run"}
Ci-Attestation: {"at":1793562864,"gate":"rust/x86_64-pc-windows-msvc/dylint","host":"linux-x86_64","key":"cb88e9fcab2c55994b3ec27de674531ca9553155a446b5924672d4f18e0552e0","lane":"cross","parents":["a8f30ed6f587707dee2677413a9e0419adc7264d"],"secs":38,"stamp":"7e0cfdd270b31e2f74b4960f19a95015","tree":"c6439dc37a0145fa5bc5028137f2ff1b1b6c424e","v":1,"via":"run"}
Ci-Attestation: {"at":1793562864,"gate":"rust/aarch64-apple-darwin/clippy","host":"linux-x86_64","key":"cb88e9fcab2c55994b3ec27de674531ca9553155a446b5924672d4f18e0552e0","lane":"cross","parents":["a8f30ed6f587707dee2677413a9e0419adc7264d"],"secs":38,"stamp":"d0c4ec32a4c02f59e541dc30345af231","tree":"c6439dc37a0145fa5bc5028137f2ff1b1b6c424e","v":1,"via":"run"}
Ci-Attestation: {"at":1793562864,"gate":"rust/aarch64-apple-darwin/dylint","host":"linux-x86_64","key":"cb88e9fcab2c55994b3ec27de674531ca9553155a446b5924672d4f18e0552e0","lane":"cross","parents":["a8f30ed6f587707dee2677413a9e0419adc7264d"],"secs":38,"stamp":"fd6ed26841f820aac6456c6dbe5aab41","tree":"c6439dc37a0145fa5bc5028137f2ff1b1b6c424e","v":1,"via":"run"}
Ci-Attestation: {"at":1793562864,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"470964d90453530511fbad59c537a129df93ee9028f232f3691bc0f3201b6e92","lane":"tier2-linux","parents":["a8f30ed6f587707dee2677413a9e0419adc7264d"],"secs":266,"stamp":"ceaaa5181f74c96bf81243c9b5a0190d","tree":"c6439dc37a0145fa5bc5028137f2ff1b1b6c424e","v":1,"via":"run"}

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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: libobs: LeakSanitizer reports the context control of scenes alive at obs_shutdown (follow-up to #2038)

2 participants