[STF] Unify and harden the ctx callback-args resources #11229
All Vouch review confidence checks passed
Residual risk 0.19 = Change Risk (0.43) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.43 · 391 lines diff across 4 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.357
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #11229"
general
weight: 0.35
caugonnet
Summary
parallel_for_args_resourceandhost_callback_args_resourcewere the only twoctx_resourcesubclasses in the tree and, apart from their template-parameter and member names, identical. They are replaced by a singlereserved::callback_args_resource<Payload>inctx_resource.cuh, next to the base class and the resource set.Along the way this fixes a real use-after-free window and records an ownership rule that is easy to get wrong — as this PR itself demonstrated.
The ownership rule
The payload outlives the call that enqueues the callback, so it cannot live on the stack. It may be referenced by a graph node that is replayed more than once, so the callback must not free it. And critically:
The payload must outlive any asynchronous work referencing it, and only the stream-ordered release callback knows when that work has drained. A destructor cannot know.
mix_stream_and_graph.cuis the counter-example: it builds agraph_ctxinside a stream task, callssubmit(stream), and lets the context go out of scope with no sync and nofinalize(). Sincerelease_ctx_resources()runs only fromfinalize()and~impl()is empty, the resource set is destroyed while the graph is still executing, with a host node whoseuserDatapoints at the payload. Freeing there segfaults. Leaking keeps the payload alive for the pending callback — so the leak is load-bearing.An earlier revision of this PR made the resource hold a
unique_ptr, which freed in the destructor and crashed exactly that test across T4, H100 and 2-GPU jobs. The constructor still takes aunique_ptr, so the handoff stays exception-safe (the caller owns the payload until the resource exists), but the member is a raw pointer and the destructor deliberately does nothing. The reason and the failing test are recorded in the comment so this is not "fixed" again.Register ownership before creating the graph node
Both graph branches in
host_launch_scope.cuhcreated the host node first and registered the resource second, so a throw fromctx.add_resource()left a live graph node pointing at a payload that the caller'sunique_ptrthen freed at scope exit. Registering first removes the window: a throw fromcudaGraphAddHostNodeleaves the payload owned by the context with no node referencing it, and a throw fromadd_resourcehappens before any node exists.parallel_foralready did it in this order;host_launchnow matches.Thanks @coderabbitai for catching this one.
Also
context.cuhdefined a byte-identicaldummy_released_resourceinside three consecutiveUNITTESTs. Defined once, innamespace reserved. The region already sits inside the file's#ifdef UNITTESTED_FILEguard, so it stays test-only.parallel_forpasses the resource as an unnamed temporary instead of naming a variable that had to be moved from exactly once.Note on overlap
Touches
parallel_for_scope.cuhnear the region #11224 changes, so whichever lands second needs a trivial rebase.🤖 Generated with Claude Code