[AI] Fix object mask concurrency and shape-lifetime bugs#20815
Merged
TurboGit merged 2 commits intodarktable-org:masterfrom Apr 15, 2026
Merged
[AI] Fix object mask concurrency and shape-lifetime bugs#20815TurboGit merged 2 commits intodarktable-org:masterfrom
TurboGit merged 2 commits intodarktable-org:masterfrom
Conversation
This was referenced Apr 14, 2026
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.
Two open PRs by @sjuxax have been awaiting a response from the author for a while. I don't consider everything in those PRs meaningful for merging, but a couple of fixes are genuinely worth having. This PR ports only those.
The originals:
#20774 – Fix object mask ASan races and scratch overflows
_run_decoderthread-join fix. Real concurrency bug.malloc/freereplacements ofdt_get_perthread()inbox_filters.ccandguided_filter.c. These patch symptoms rather than the root cause (likely a pool-sizing issue indt_alloc_perthread_float), and the hot-path allocator churn would regress performance for every user, not just those running ASan.#20775 – Fix SAM2 segmentation ASan crashes
DT_AI_OPT_DISABLEDfor the SAM2.1 encoder. This disables ORT graph optimization for all SAM2 users to work around an ORT 1.24.x UAF that may be ASan-only, at a measurable inference-performance cost. Worth revisiting if confirmed to crash in release builds.What's in this PR
Wait for encode thread before decoding.
ENCODE_READYis published before the background warmup finishes, so a fast user click could race the warmup on the shareddt_seg_context_t._run_decoder()now joins the thread first – immediate no-op if warmup already completed, brief pause otherwise. Related comment at the ready-signal site was updated to reflect the new behavior.Keep decoder output shape arrays alive through
dt_ai_run. Indt_seg_warmup_decoderanddt_seg_compute_mask,iou_shape/lr_shape/low_res_shapewere declared inside an innerif(is_sam)block while pointers to them were stored in anoutputs[]array from the outer scope. Per C, their lifetime ended at the closing}of the inner block – thedt_ai_run()call afterward read through dangling pointers. Works in release today because compilers rarely recycle stack slots between sibling scopes, but caught by ASan and a latent time bomb for future compiler versions. Moved to the outer scope; zero runtime cost.