Skip to content

[core] Ensure we free objects whose location report arrives after the ref got dropped - #66383

Merged
Sparks0219 merged 4 commits into
ray-project:masterfrom
Yicheng-Lu-llll:free-late-reported-copies
Sep 23, 2026
Merged

Sparks0219 merged 4 commits into
ray-project:masterfrom
Yicheng-Lu-llll:free-late-reported-copies

Conversation

@Yicheng-Lu-llll

@Yicheng-Lu-llll Yicheng-Lu-llll commented Sep 22, 2026 •

Copy link
Copy Markdown
Member

Description

We had a race condition that caused objects to not get freed:

  • owner call ray.get
  • raylet finishes pulling object and reports the new location to the owner. Assume this report remains in flight.
  • owner finds the object, finishes ray.get, and then drops the ref immediately, triggering object freeing. The owner only sends FreeLocalObjects to the locations it has recorded at that moment. raylet's report hasn't arrived yet.
  • When the report arrives, the owner no longer has an entry for that object and discards it.

The fix is simple. we send FreeLocalObjects if there is no object entry in the owner.

@Yicheng-Lu-llll
Yicheng-Lu-llll marked this pull request as ready for review September 22, 2026 22:02
@Yicheng-Lu-llll
Yicheng-Lu-llll requested a review from a team as a code owner September 22, 2026 22:02
@Yicheng-Lu-llll Yicheng-Lu-llll added core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests labels Sep 22, 2026

@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 addresses an issue where object copies could be leaked if a location report arrives after the reference has already been dropped. It introduces an asynchronous free of the object on nodes when the reference does not exist in CoreWorker::AddObjectLocationOwner, and adds a corresponding unit test. However, a review comment identifies a potential memory leak under a specific race condition where maybe_generator_id is not Nil() but the reference does not exist, causing the code to skip the newly added else if block. It is recommended to update reference_exists with the result of AddObjectLocation and perform the !reference_exists check unconditionally after the block.

Comment thread src/ray/core_worker/core_worker.cc Resolved
Comment thread src/ray/core_worker/core_worker.cc Outdated
}
if (!reference_counter_->AddObjectLocation(object_id, node_id)) {
// The ref may be dropped and free objects sent before this report arrives, so free
// the additional objects here.

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.

nit: "free the additional objects here" -> "free the additional copies here"

@Sparks0219 Sparks0219 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.

🚢

Signed-off-by: yicheng <[email protected]>
@Sparks0219
Sparks0219 enabled auto-merge (squash) September 23, 2026 00:19
@Sparks0219
Sparks0219 merged commit e2a353b into ray-project:master Sep 23, 2026
7 checks passed
dstrodtman added a commit that referenced this pull request Sep 24, 2026
## Why are these changes needed?

Since about 2026-09-23 04:45 UTC, Read the Docs builds on `anyscale-ray`
have been intermittently dying with a bare segfault in `sphinx-build`:

```
[autosummary] generating autosummary for: 404.md, ...
make: *** [Makefile:100: html] Segmentation fault (core dumped)
```

What I found in the build history:

- None of the 132 finished builds from 2026-09-18 up to the onset
segfaulted. There have been 23 since, on both `master` and PR previews
across at least 10 PRs. In one 13-hour window, 10 of 32 finished builds
crashed.
- Every crash is at the same point, about 6 to 7 seconds into the `make
html` step, right after autosummary starts. This isn't the memory-limit
failure that the `SPHINX_JOBS=2` comment describes, which surfaces as an
`EOFError` from a worker much later in the build.
- It isn't deterministic per commit. `master` at 8a94f8b segfaulted in
builds 4473269 and 4474033, then passed in 4474041. A line-by-line diff
of the failing and passing logs shows the same commands, the same Python
(3.11.15), and the same installed package versions.
- No doc-build input changed in the onset window. The only commit that
landed on `master` in it was a core C++ change (#66383), which the docs
build doesn't compile.

That points to a change in the build environment rather than in this
repo, but the log gives nothing to hand to Read the Docs support or to
act on. This PR sets `PYTHONFAULTHANDLER=1` on the html build step so
the next crash dumps the Python stack of every thread to stderr. That
should name the module or C extension that crashed. The handler only
does anything when a fatal signal arrives, so passing builds are
unaffected.

Once the cause is found and fixed, this can stay as cheap insurance or
be reverted.

## Related issue number

None. I searched open and closed PRs for `faulthandler`, `readthedocs
segfault`, and `Segmentation fault`, and none of them cover the docs
build. #66364 fixes a segfault on `import ray` when a `RAY_*` env var is
malformed, which is a different path in core's static initialization.

## Testing

- Ran a deliberately segfaulting Python child through a `make` recipe,
the same way the build invokes `sphinx-build`. With
`PYTHONFAULTHANDLER=1`, make's output includes `Fatal Python error:
Segmentation fault` and the Python traceback. Without it, the output is
only make's `Segmentation fault` line.
- `python3 -c "import yaml; yaml.safe_load(open('.readthedocs.yaml'))"`
parses, and the html build command reads back as expected.
- `pre-commit run --files .readthedocs.yaml` passes.
- The Read the Docs preview for this PR runs the modified step, because
the preview guard builds whenever `.readthedocs.yaml` changes.

## AI assistance

I used Claude Code to survey the Read the Docs build history through the
RtD API, diff the build logs, and draft this change. I reviewed the diff
and the evidence above.

## Checks

- [x] I've signed off every commit (by using the -s flag, i.e., `git
commit -s`) in this PR.
- [x] I've run pre-commit jobs to lint the changes in this PR.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Douglas Strodtman <[email protected]>
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
… ref got dropped (ray-project#66383)

## Description

We had a race condition that caused objects to not get freed:
- owner call `ray.get`
- raylet finishes pulling object and reports the new location to the
owner. Assume this report remains in flight.
- owner finds the object, finishes `ray.get`, and then drops the ref
immediately, triggering object freeing. The owner only sends
`FreeLocalObjects` to the locations it has recorded at that moment.
raylet's report hasn't arrived yet.
- When the report arrives, the owner no longer has an entry for that
object and discards it.

The fix is simple. we send `FreeLocalObjects` if there is no object
entry in the owner.

---------

Signed-off-by: yicheng <[email protected]>
Co-authored-by: yicheng <[email protected]>
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
…ject#66452)

## Why are these changes needed?

Since about 2026-09-23 04:45 UTC, Read the Docs builds on `anyscale-ray`
have been intermittently dying with a bare segfault in `sphinx-build`:

```
[autosummary] generating autosummary for: 404.md, ...
make: *** [Makefile:100: html] Segmentation fault (core dumped)
```

What I found in the build history:

- None of the 132 finished builds from 2026-09-18 up to the onset
segfaulted. There have been 23 since, on both `master` and PR previews
across at least 10 PRs. In one 13-hour window, 10 of 32 finished builds
crashed.
- Every crash is at the same point, about 6 to 7 seconds into the `make
html` step, right after autosummary starts. This isn't the memory-limit
failure that the `SPHINX_JOBS=2` comment describes, which surfaces as an
`EOFError` from a worker much later in the build.
- It isn't deterministic per commit. `master` at 8a94f8b segfaulted in
builds 4473269 and 4474033, then passed in 4474041. A line-by-line diff
of the failing and passing logs shows the same commands, the same Python
(3.11.15), and the same installed package versions.
- No doc-build input changed in the onset window. The only commit that
landed on `master` in it was a core C++ change (ray-project#66383), which the docs
build doesn't compile.

That points to a change in the build environment rather than in this
repo, but the log gives nothing to hand to Read the Docs support or to
act on. This PR sets `PYTHONFAULTHANDLER=1` on the html build step so
the next crash dumps the Python stack of every thread to stderr. That
should name the module or C extension that crashed. The handler only
does anything when a fatal signal arrives, so passing builds are
unaffected.

Once the cause is found and fixed, this can stay as cheap insurance or
be reverted.

## Related issue number

None. I searched open and closed PRs for `faulthandler`, `readthedocs
segfault`, and `Segmentation fault`, and none of them cover the docs
build. ray-project#66364 fixes a segfault on `import ray` when a `RAY_*` env var is
malformed, which is a different path in core's static initialization.

## Testing

- Ran a deliberately segfaulting Python child through a `make` recipe,
the same way the build invokes `sphinx-build`. With
`PYTHONFAULTHANDLER=1`, make's output includes `Fatal Python error:
Segmentation fault` and the Python traceback. Without it, the output is
only make's `Segmentation fault` line.
- `python3 -c "import yaml; yaml.safe_load(open('.readthedocs.yaml'))"`
parses, and the html build command reads back as expected.
- `pre-commit run --files .readthedocs.yaml` passes.
- The Read the Docs preview for this PR runs the modified step, because
the preview guard builds whenever `.readthedocs.yaml` changes.

## AI assistance

I used Claude Code to survey the Read the Docs build history through the
RtD API, diff the build logs, and draft this change. I reviewed the diff
and the evidence above.

## Checks

- [x] I've signed off every commit (by using the -s flag, i.e., `git
commit -s`) in this PR.
- [x] I've run pre-commit jobs to lint the changes in this PR.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Douglas Strodtman <[email protected]>
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants