← Back to Pull requests

[STF] Unify and harden the ctx callback-args resources #11229

Closed
unknown closed this pull request without merging on Sep 07, 2026 04:18 UTC
View on GitHub
unknown unknown commented on Sep 06, 2026 01:56 UTC
Author

Summary

parallel_for_args_resource and host_callback_args_resource were the only two ctx_resource subclasses in the tree and, apart from their template-parameter and member names, identical. They are replaced by a single reserved::callback_args_resource<Payload> in ctx_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 destructor must not free it either.

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.cu is the counter-example: it builds a graph_ctx inside a stream task, calls submit(stream), and lets the context go out of scope with no sync and no finalize(). Since release_ctx_resources() runs only from finalize() and ~impl() is empty, the resource set is destroyed while the graph is still executing, with a host node whose userData points 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 a unique_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.cuh created the host node first and registered the resource second, so a throw from ctx.add_resource() left a live graph node pointing at a payload that the caller's unique_ptr then freed at scope exit. Registering first removes the window: a throw from cudaGraphAddHostNode leaves the payload owned by the context with no node referencing it, and a throw from add_resource happens before any node exists. parallel_for already did it in this order; host_launch now matches.

Thanks @coderabbitai for catching this one.

Also

  • context.cuh defined a byte-identical dummy_released_resource inside three consecutive UNITTESTs. Defined once, in namespace reserved. The region already sits inside the file's #ifdef UNITTESTED_FILE guard, so it stays test-only.

  • parallel_for passes 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.cuh near the region #11224 changes, so whichever lands second needs a trivial rebase.

    🤖 Generated with Claude Code

    Diff: 391 lines (+293 / -98) across 4 files Reviewer: caugonnet Review duration: 1581m
    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
    0.43
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 43.1% (MEDIUM) · Higher = larger blast radius
    43.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 18.6% · Safe to merge
    18.6%
    Repository