Skip to content

[cherry-pick][2.57.1][Data] Don't register out-of-scope callbacks on blocks passed through from input to output (#66369) - #66403

Merged
elliot-barn merged 1 commit into
releases/2.57.1from
cherry-pick-66369-2.57.1
Sep 23, 2026
Merged

elliot-barn merged 1 commit into
releases/2.57.1from
cherry-pick-66369-2.57.1

Conversation

@elliot-barn

Copy link
Copy Markdown
Collaborator

Cherry-pick of #66369 (master commit 428f0dd) onto releases/2.57.1, the new patch-release branch cut from releases/2.57.0 (this PR sits on top of the bumping ray version to 2.57.1 commit).

Fixes the streaming_split over a materialized / from_* dataset followed by randomize_block_order crash (Cannot register an out-of-scope/freed callback for object ...: it is not owned by this worker). The bug shipped with #64157 in 2.57 and was reported from a Ray Train job on 2.58.0.

Applied cleanly with git cherry-pick -x. block_ref_counter.py on this branch predates a small unrelated refactor on master, so the two docstring/TODO hunks landed at different line offsets, but the content is identical to the master commit; the other three files were byte-identical to master's parent. Not re-run locally (no Ray build for the release branch on this box); relying on the branch CI for python/ray/data/tests/test_operators.py and test_streaming_integration.py, which passed on master in #66369. Same change as #66401 for 2.59.0.

🤖 Generated with Claude Code

… from input to output (#66369)

## Description

A Ray Train job dies before it reads a single row. Its input pipeline
shuffles a dataset that was already materialized:

```python
ds = ray.data.range(100, override_num_blocks=10).materialize()
ds = ds.randomize_block_order()
(it,) = ds.streaming_split(1, equal=True)
list(it.iter_rows())
```

```
(SplitCoordinator pid=42263) Split 0 epoch 0 get() failed after 0 rows: Cannot register
an out-of-scope/freed callback for object 6f1bcddfc6570c62…: it is not owned by this
worker (it may be owned by another worker, or have no ownership record). These callbacks
can only be registered by the owner.
```

### Why it happens

`BlockRefCounter` tracks object-store memory. To do that, it asks Ray
Core to call it back when a block is freed. Only the block's owner may
register that callback, and the owner is the process that created the
block.

`streaming_split` runs the plan inside a `SplitCoordinator` actor. But
the blocks were created earlier, by `materialize()` on the caller. The
same holds for any `InputData` source, such as `from_arrow` or
`from_pandas`.

```
  caller                        SplitCoordinator actor
  ──────────────                ──────────────────────
  materialize()   ── refs ──▶   randomize_block_order → … → split(N)
  owns B1..B10                  owns nothing it was handed
```

`AllToAllOperator.all_inputs_done` registered a callback for every block
it sent downstream. That is right for sort, shuffle and repartition.
Their output blocks come from reduce tasks the operator itself
submitted, so it owns them.

It is wrong for `RandomizeBlocks`. That operator creates no blocks. It
reorders its input bundles and returns the same refs. So the actor asks
Ray Core to watch blocks the caller owns, Ray Core refuses, and the
execution fails.

### The fix

Register a block only when its ref was not among the operator's inputs:

```python
input_refs = {entry.ref for b in input_bundles for entry in b.blocks}
...
for bundle in output_buffer:
    for entry in bundle.blocks:
        if entry.ref in input_refs:
            continue
        self._block_ref_counter.on_block_produced(...)
```

Returned refs are skipped. New ones are still registered.
`OutputSplitter` already uses the same test on `_split_block` output.

**Why not `RefBundle.owns_blocks`?** That flag says whether the blocks
may be destroyed, not who made them. The shuffle, sort and
split-repartition schedulers copy it from their input onto blocks their
own reduce tasks made (`input_owned = all(b.owns_blocks for b in refs)`
at `pull_based_shuffle_task_scheduler.py:50`, used at `:145`). A filter
on that flag would drop real blocks from the counter whenever the source
is `from_*` or `materialize`, and under-report their memory. Bugbot
caught this in the first version of this PR.

### Where it came from

#64157 added `BlockRefCounter` to the operators in 2.57, and the crash
has been there since. It was reported from a Ray Train job on 2.58.0.
That job's input pipeline is `from_arrow → repartition → materialize →
randomize_block_order → map_batches → repartition → streaming_split`,
and all four splits fail at 0 rows.

## Related issues

None open. I searched ray-project/ray for open PRs and issues mentioning
`BlockRefCounter`, `owns_blocks`, `streaming_split materialized`, and
the error text "Cannot register an out-of-scope callback". Two open PRs
touch the counter: #65878 (consumer-held bytes) and #65220 (deferring
gRPC frees). Neither changes this registration path, and no open issue
tracks the crash, so this is not duplicate work.

## Additional information

**Tests.** `test_streaming_split_over_materialized_all_to_all`
reproduces the failure end to end. The two tests in `test_operators.py`
cover both sides of the check; the second fails if the check uses
`owns_blocks`, which is what the first version of this PR did.

```
$ PYTHONPATH=python/ray/data/tests python -m pytest \
    python/ray/data/tests/test_streaming_integration.py \
    -k test_streaming_split_over_materialized_all_to_all -q
1 passed, 25 deselected in 9.28s        # fails on master with the error above

$ PYTHONPATH=python/ray/data/tests python -m pytest \
    python/ray/data/tests/test_operators.py \
    python/ray/data/tests/test_randomize_block_order.py \
    python/ray/data/tests/unit/test_block_ref_counter.py \
    python/ray/data/tests/unit/test_resource_manager.py -q
29 passed in 16.10s

$ PYTHONPATH=python/ray/data/tests python -m pytest \
    python/ray/data/tests/test_streaming_integration.py -k streaming_split -q
13 passed, 13 deselected in 68.67s
```

The last command needs #66366 applied locally. On master,
`request_remaining=True` in `rate_based_cluster_autoscaler.py` raises
`TypeError: 'bool' object is not iterable` in the autoscaling
coordinator, which makes these tests flaky.

**CI.** The `buildkite/premerge` failure here is that same master bug.
All three pyrefly errors are in `rate_based_cluster_autoscaler.py`, a
file this PR does not touch. #66366 and #66370 both fix it, and this PR
is unblocked once either one lands.

**AI assistance was used** for the diagnosis, the patch, and this
description.

---------

Signed-off-by: Marwan Sarieddine <[email protected]>
Co-authored-by: Ayush Kumar <[email protected]>
(cherry picked from commit 428f0dd)
Signed-off-by: elliot-barn <[email protected]>
@elliot-barn
elliot-barn requested a review from a team as a code owner September 23, 2026 07:05

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request prevents registering out-of-scope callbacks on forwarded input blocks that are not owned by the current worker, which previously caused execution failures (e.g., when running streaming_split over a materialized dataset with randomized block order). It updates AllToAllOperator to skip calling on_block_produced for forwarded blocks and adds corresponding unit and integration tests. I have no additional feedback to provide.

@elliot-barn
elliot-barn merged commit 0939e8e into releases/2.57.1 Sep 23, 2026
4 of 5 checks passed
@elliot-barn
elliot-barn deleted the cherry-pick-66369-2.57.1 branch September 23, 2026 07:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants