← Back to Pull requests

Explicit lifetime for the streaming collectives #1316

Closed
unknown closed this pull request without merging on Nov 13, 2026 07:38 UTC
View on GitHub
unknown unknown commented on Nov 05, 2026 13:21 UTC
Author

Closes #1311, together with #1315.

The Python streaming collectives ShufflerAsync, SparseAlltoall and AllGather had no way to be released. Unextracted data, the progress function and the spill function lived until __dealloc__, which in async code can be much later. In cudf#25074 a failed query kept two completed shufflers alive in a reference cycle.

This PR adds aclose() to all three classes, and async with to ShufflerAsync and SparseAlltoall:

async with ShufflerAsync(ctx, comm, op_id, n) as shuffler:
    shuffler.insert(chunks)
    await shuffler.insert_finished()
    for pid in shuffler.local_partitions():
        parts = shuffler.extract(pid)

When aclose() returns, the collective has completed on this rank, its resources are released and the op_id may be reused. Unextracted data is freed. If insert_finished() was not called, aclose() inserts the finish markers so that peers do not hang, closes the object and raises RuntimeError, since skipping insert_finished() is a bug in the caller. __aexit__ does not raise that error when the body raised, so the body's exception propagates unchanged. aclose() waits for every peer. Cancelling it gives control back while the collective keeps finishing in the background, and awaiting aclose() again waits for the same completion.

The Cython wrappers now hold the C++ objects in a shared_ptr, and every background task holds its own reference. A cancelled Python awaiter or a dropped wrapper therefore no longer leaves a task pointing at a destroyed object. The three wrappers derive from a new public abstract base class, rapidsmpf.streaming.coll.Collective, which provides aclose() and can be used to type or check for any streaming collective. cpp_set_py_future now leaves a future alone when its event loop has been closed, instead of reporting an ignored exception.

Breaking change

In C++, streaming::ShufflerAsync::insert_finished() and streaming::SparseAlltoall::insert_finished() are now synchronous and only insert the finish markers. Awaiting completion is a separate wait() coroutine, which streaming::AllGather also gets. All three classes now follow the same pattern as the non-streaming collectives, insert_finished() followed by wait(). The split lets the Python wrappers raise errors from insert_finished() at the call, and lets several tasks await completion, which aclose() needs after a cancelled insert_finished().

// Before
co_await shuffler.insert_finished();
// After
shuffler.insert_finished();
co_await shuffler.wait();

No deprecation period is needed. Outside this repository, the only caller is the ndsh join benchmark in libcudf_streaming, which needs a one-line change once this PR is in a nightly (NVIDIA/cudf PR to follow). Old code fails to compile instead of changing behavior, since a void cannot be awaited.

The Python API is not broken. The wrappers now store the Context they were created with, so the ctx argument of ShufflerAsync.insert_finished(), SparseAlltoall.insert_finished() and AllGather.extract_all() is optional and ignored. It will be removed once cudf-polars no longer passes it.

Naming

aclose() is the first method of that name in rapidsmpf. Elsewhere we use shutdown(), for example the async Channel.shutdown() and MemoryReserveOrWait.shutdown(), and the C++ Shuffler::shutdown(). I chose aclose() because it is the Python protocol for closing an async resource: async generators, contextlib.AsyncExitStack, httpx, anyio and Trio use it, and contextlib.aclosing() calls it. Channel.shutdown() also does something different, since it stops the channel without waiting for anything. What do you guys think?

Diff: 487 lines (+365 / -122) across 6 files Reviewer: pentschev Review duration: 11176m
All Vouch review confidence checks passed
Residual risk 0.19 = Change Risk (0.47) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.47 · 487 lines diff across 6 files
0.47
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%
60%
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.370
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #1316"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% 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: 47.4% (MEDIUM) · Higher = larger blast radius
47.4%
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: 60.5% · Lower = rushed or shallow review
60.5%
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.7% · Safe to merge
18.7%