Repository navigation
fix(libobs): obs_shutdown() lets a scene release its items before it destroys what is left (#3441) - #3442
Merged
Conversation
…e it destroys what is left (#3441) obs_free_data() releases the main canvas, which queues the destroy of the scene it held on the destruction task thread. Since #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]>
…ore it destroys what is left (#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 #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"}
zackees
approved these changes
Nov 1, 2026
zackees
left a comment
Member
There was a problem hiding this comment.
Reviewed. Draining the destruction queue before the left-alive loop and pinning each remaining control block with a weak ref closes the #3441 use-after-free; the obs_weak_ref_expired check correctly separates sources destroyed directly (their strong-side weak ref still held) from those released by a deferred scene teardown. The RED case extends a repo-added test (08aed4e), so it's within policy. LGTM; I'll queue it once the last check is green.
This was referenced Nov 1, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3441. Regression from #3431 (#3364), which is in
obs-studio-rust-v0.1.0-beta.20261101.What
obs_free_data()releases the main canvas, which queues the destroy of the scene it held on the destruction task thread. Since #3431 the left-alive loop frees the control block of each source it destroys. The scene's inputs stay in the source table until the scene's deferred destroy releases its items. When the loop reaches them first, the scene's teardown releases freed control blocks: a heap-use-after-free under ASan (test_source_profiler).The app's own close path is not affected:
OBSBasiccallsClearSceneData()beforeobs_shutdown(), which sets every output channel tonullptrand releases every scene and source. Only code that calls libobs directly and leaves a scene in the canvas at shutdown reaches it (tests, embedders, a plugin with a leak). This is the low-risk tier of SECURITY.md.Fix
obs_free_data()waits until the destruction queue is empty before the left-alive loop. The scene's teardown then releases its items throughobs_source_release(), which frees each control block in the normal way. Only the sources the host left alive stay in the tables.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 that the loop starts (a scene the host left alive releasing its items) can drop a source's last strong reference. The hold keeps the block valid until then, and no block is released twice. The final wait also repeats until the queue is empty.RED → GREEN
test_scene_items_are_released_not_left_aliveintest_shutdown_canvas_weak_ref. A scene held only by the main canvas holds two inputs; a 200 ms delay in the scene's firstitem_removesignal (on the destruction thread, before the items are released) forces the order. It synchronizes nothing. Without ASan the use-after-free is invisible, so the case checks that shutdown logs no "source(s) were remaining".test_shutdown_canvas_weak_reffails, onleft_alive_logged is not false.test_source_profiler.test_source_profilerandtest_shutdown_type_lifetime. The two fix(libobs): obs_shutdown() frees the control block of a source left alive (#3364) #3431 cases still pass (nothing left after shutdown; a held weak reference frees on release).Not in this PR
Two older hazards in the same loop are independent of #3431 and only reachable when the host leaks a scene at shutdown. They go to a separate low-priority issue: the unlocked iteration of the source tables while the destruction thread removes entries, and a leaked item's deferred destroy freeing the source before a leaked scene's teardown releases it.
🤖 Generated with Claude Code