Skip to content

[Core] Fix EvictLineage evicting all lineage due to negative byte accounting - #67271

Merged
rueian merged 1 commit into
ray-project:masterfrom
dragongu:fix/evict-lineage-negative-bytes
Oct 26, 2026
Merged

rueian merged 1 commit into
ray-project:masterfrom
dragongu:fix/evict-lineage-negative-bytes

Conversation

@dragongu

Copy link
Copy Markdown
Contributor

Description

The lineage-release callback returned the byte delta with the wrong sign, so eviction never reached its target and removed the entire reconstructable-object lineage. Under memory pressure, one eviction wiped lineage for every in-flight object while freeing only ~835MB; a node death seven minutes later left 13 objects unrecoverable and failed the job with OBJECT_UNCONSTRUCTABLE_LINEAGE_EVICTED.

The fix reports released bytes with the correct sign so eviction stops once it has released enough lineage.

Related issues

N/A.

Additional information

Production evidence

RAY_max_lineage_bytes was set to 20GiB. The job failed with this sequence:

[2026-10-22 10:09:34,054 I ...] task_manager.cc:1149: Total lineage size is 21474.8MB, which exceeds the limit of 21474.8MB
[2026-10-22 10:09:34,757 I ...] task_manager.cc:1213: Evicted -835.169MB of task lineage.
...
[2026-10-22 10:16:18,617 I ...] Attempting to recover 115 lost objects by resubmitting their tasks...
[2026-10-22 10:16:18,618 I ...] Cannot recover object abf8e6489a7fc95e...0200000002000000: OBJECT_UNCONSTRUCTABLE_LINEAGE_EVICTED
... (13 objects total)
...
ray.exceptions.RayTaskError(ObjectReconstructionFailedError): ray::Filter(<lambda>)() (pid=..., ip=<worker-ip>)
[OBJECT_UNCONSTRUCTABLE_LINEAGE_EVICTED] The object cannot be reconstructed because its lineage has been evicted to reduce memory pressure.
To prevent this error, set the environment variable RAY_max_lineage_bytes=<bytes> (default 1GB) during `ray start`.
...
SystemException

Root cause

ReferenceCounter::EvictLineage() accumulates the callback result and stops only when the accumulator reaches a positive target:

int64_t lineage_bytes_evicted = 0;
while (!reconstructable_owned_objects_.empty() &&
       lineage_bytes_evicted < min_bytes_to_evict) {
  ...
  lineage_bytes_evicted += ReleaseLineageReferences(it);
}

ReleaseLineageReferences() calls TaskManager::RemoveLineageReference() and adds its return value. That callback was returning the net change of total_lineage_footprint_bytes_ (new - prev). Releasing lineage can only decrease the total, so the value is always <= 0. Since min_bytes_to_evict = total - max / 2 is positive, the accumulator could never reach the target, and the only way out of the loop was an empty queue.

The accounting error is only half of the impact. Each popped object is also marked INELIGIBLE_LINEAGE_EVICTED if it is still in scope and eligible, even when its task spec cannot be freed yet (for example, another return is still in scope). The queue was therefore drained — every in-flight eligible object lost lineage — while only the task specs whose last in-scope return was in the queue were actually freed.

The failure is latent: it surfaces on the next node death for an object with no other copy, when reconstruction is refused with OBJECT_UNCONSTRUCTABLE_LINEAGE_EVICTED.

Fix

Return prev - current, the non-negative number of bytes released, matching the LineageReleasedCallback contract.

Test plan

  • bazel build //src/ray/core_worker/tests:task_manager_test
  • bazel-bin/src/ray/core_worker/tests/task_manager_test --gtest_filter=TaskManagerLineageEvictionTest.TestEvictLineageStopsAtTarget
    • [ PASSED ] 1 test.
  • Reverting the return to current - prev makes the test fail.

The new test uses two completed retryable tasks. EvictLineage(1) must report positive released bytes, free only the oldest task's lineage, and leave the newer object ELIGIBLE — proving the queue was not drained.

Follow-up

This fixes the sign error, not the footprint/queue accounting. EvictLineage() can still drain the queue when total_lineage_footprint_bytes_ and reconstructable_owned_objects_ are out of sync (e.g. streaming-generator return IDs re-inserted at completion after consumption; see the TODO in task_manager.cc). That should be tracked separately.

Note

The OBJECT_UNCONSTRUCTABLE_LINEAGE_EVICTED message does suggest increasing RAY_max_lineage_bytes, but python/ray/exceptions.py hardcodes (default 1GB) regardless of the configured value (20GiB in this incident). That makes the guidance misleading during diagnosis. It will be fixed in a separate PR and is intentionally not changed here.

@dragongu
dragongu requested a review from a team as a code owner October 22, 2026 08:10

@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 fixes a bug in TaskManager::RemoveLineageReference where it returned a negative delta instead of the positive number of bytes released, which caused the eviction loop to drain the entire queue. A unit test was added to verify that eviction stops at the target. The reviewer suggested defensively wrapping the returned value in std::max to guarantee it is never negative.

Comment thread src/ray/core_worker/task_manager.cc Resolved
…ounting

TaskManager::RemoveLineageReference() returned the net change of
total_lineage_footprint_bytes_ (new - prev). Releasing lineage can only
decrease that total, so the value was always <= 0. EvictLineage() treats it as
"bytes evicted" and loops while lineage_bytes_evicted < min_bytes_to_evict
(always positive at the call site), so the accumulator could never reach the
target. The only remaining exit was an empty queue: whenever eviction was
triggered, it evicted every entry in reconstructable_owned_objects_ instead of
stopping after enough lineage was released.

Consequence: every eligible, in-scope object in the queue was marked
INELIGIBLE_LINEAGE_EVICTED, while only tasks whose in-scope return IDs were all
present in the queue were actually freed. Once the limit was hit, the next node
death could leave an object with neither a copy nor lineage, and reconstruction
failed with OBJECT_UNCONSTRUCTABLE_LINEAGE_EVICTED.

Fix: return the negated delta (bytes released, non-negative), matching the
LineageReleasedCallback contract. The accumulator can then reach
min_bytes_to_evict and the loop stops after a partial eviction.

Test: add TaskManagerLineageEvictionTest.TestEvictLineageStopsAtTarget, which
fails with the previous return value (queue drained) and passes with the fix.

Signed-off-by: dragongu <[email protected]>
@dragongu
dragongu force-pushed the fix/evict-lineage-negative-bytes branch from 591657c to 5be279d Compare October 22, 2026 09:39
@ray-gardener ray-gardener Bot added core Issues that should be addressed in Ray Core community-contribution Contributed by the community labels Oct 22, 2026
@rueian rueian self-assigned this Oct 23, 2026
@rueian rueian added the go add ONLY when ready to merge, run all tests label Oct 23, 2026

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

Thanks for making this fix!!! Now lineage reconstruction won't drop lineages for no reason 🤣

@rueian
rueian merged commit fb7c757 into ray-project:master Oct 26, 2026
7 checks passed
elliot-barn added a commit that referenced this pull request Oct 28, 2026
MSVC rejects the lambda's implicit use of the local const max_retries
(C3493: cannot be implicitly captured), so task_manager_test fails to
build on Windows since #67271. Pass -1 directly instead.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: elliot-barn <[email protected]>
justinyeh1995 pushed a commit to justinyeh1995/ray that referenced this pull request Oct 28, 2026
…ounting (ray-project#67271)

## Description

The lineage-release callback returned the byte delta with the wrong
sign, so eviction never reached its target and removed the entire
reconstructable-object lineage. Under memory pressure, one eviction
wiped lineage for every in-flight object while freeing only ~835MB; a
node death seven minutes later left 13 objects unrecoverable and failed
the job with `OBJECT_UNCONSTRUCTABLE_LINEAGE_EVICTED`.

The fix reports released bytes with the correct sign so eviction stops
once it has released enough lineage.

## Related issues

N/A.

## Additional information

### Production evidence

`RAY_max_lineage_bytes` was set to 20GiB. The job failed with this
sequence:

```text
[2026-10-22 10:09:34,054 I ...] task_manager.cc:1149: Total lineage size is 21474.8MB, which exceeds the limit of 21474.8MB
[2026-10-22 10:09:34,757 I ...] task_manager.cc:1213: Evicted -835.169MB of task lineage.
...
[2026-10-22 10:16:18,617 I ...] Attempting to recover 115 lost objects by resubmitting their tasks...
[2026-10-22 10:16:18,618 I ...] Cannot recover object abf8e6489a7fc95e...0200000002000000: OBJECT_UNCONSTRUCTABLE_LINEAGE_EVICTED
... (13 objects total)
...
ray.exceptions.RayTaskError(ObjectReconstructionFailedError): ray::Filter(<lambda>)() (pid=..., ip=<worker-ip>)
[OBJECT_UNCONSTRUCTABLE_LINEAGE_EVICTED] The object cannot be reconstructed because its lineage has been evicted to reduce memory pressure.
To prevent this error, set the environment variable RAY_max_lineage_bytes=<bytes> (default 1GB) during `ray start`.
...
SystemException
```

### Root cause

`ReferenceCounter::EvictLineage()` accumulates the callback result and
stops only when the accumulator reaches a positive target:

```cpp
int64_t lineage_bytes_evicted = 0;
while (!reconstructable_owned_objects_.empty() &&
       lineage_bytes_evicted < min_bytes_to_evict) {
  ...
  lineage_bytes_evicted += ReleaseLineageReferences(it);
}
```

`ReleaseLineageReferences()` calls
`TaskManager::RemoveLineageReference()` and adds its return value. That
callback was returning the net change of
`total_lineage_footprint_bytes_` (`new - prev`). Releasing lineage can
only decrease the total, so the value is always `<= 0`. Since
`min_bytes_to_evict = total - max / 2` is positive, the accumulator
could never reach the target, and the only way out of the loop was an
empty queue.

The accounting error is only half of the impact. Each popped object is
also marked `INELIGIBLE_LINEAGE_EVICTED` if it is still in scope and
eligible, even when its task spec cannot be freed yet (for example,
another return is still in scope). The queue was therefore drained —
every in-flight eligible object lost lineage — while only the task specs
whose last in-scope return was in the queue were actually freed.

The failure is latent: it surfaces on the next node death for an object
with no other copy, when reconstruction is refused with
`OBJECT_UNCONSTRUCTABLE_LINEAGE_EVICTED`.

### Fix

Return `prev - current`, the non-negative number of bytes released,
matching the `LineageReleasedCallback` contract.

### Test plan

- [x] `bazel build //src/ray/core_worker/tests:task_manager_test`
- [x] `bazel-bin/src/ray/core_worker/tests/task_manager_test
--gtest_filter=TaskManagerLineageEvictionTest.TestEvictLineageStopsAtTarget`
  - `[  PASSED  ] 1 test.`
- [x] Reverting the return to `current - prev` makes the test fail.

The new test uses two completed retryable tasks. `EvictLineage(1)` must
report positive released bytes, free only the oldest task's lineage, and
leave the newer object `ELIGIBLE` — proving the queue was not drained.

### Follow-up

This fixes the sign error, not the footprint/queue accounting.
`EvictLineage()` can still drain the queue when
`total_lineage_footprint_bytes_` and `reconstructable_owned_objects_`
are out of sync (e.g. streaming-generator return IDs re-inserted at
completion after consumption; see the TODO in `task_manager.cc`). That
should be tracked separately.

### Note

The `OBJECT_UNCONSTRUCTABLE_LINEAGE_EVICTED` message does suggest
increasing `RAY_max_lineage_bytes`, but `python/ray/exceptions.py`
hardcodes `(default 1GB)` regardless of the configured value (20GiB in
this incident). That makes the guidance misleading during diagnosis. It
will be fixed in a separate PR and is intentionally not changed here.

Signed-off-by: dragongu <[email protected]>
Co-authored-by: gulonglong <[email protected]>
elliot-barn added a commit that referenced this pull request Nov 2, 2026
MSVC rejects the lambda's implicit use of the local const max_retries
(C3493: cannot be implicitly captured), so task_manager_test fails to
build on Windows since #67271. Pass -1 directly instead.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: elliot-barn <[email protected]>
elliot-barn added a commit that referenced this pull request Nov 8, 2026
MSVC rejects the lambda's implicit use of the local const max_retries
(C3493: cannot be implicitly captured), so task_manager_test fails to
build on Windows since #67271. Pass -1 directly instead.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: elliot-barn <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community 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.

3 participants