Skip to content

[core] [DO NOT MERGE] (Original FreeObjects) Integrated new NodeManager FreeLocalObjects RPC - #63117

Closed
aaronscalene wants to merge 16 commits into
ray-project:masterfrom
aaronscalene:aaron/free-objects-objm-fix
Closed

aaronscalene wants to merge 16 commits into
ray-project:masterfrom
aaronscalene:aaron/free-objects-objm-fix

Conversation

@aaronscalene

@aaronscalene aaronscalene commented May 5, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Within this multi-step PR process, our goal is to change our current FreeObjects implementation to an owner driven process. Current design of FreeObjects involves spraying the entire cluster with FreeObjects RPCs. This introduces many caveats: bloating observability with FreeObjects RPC counts, sending excessive RPCs, and worse mental model from owner-driven reference counting.

Our current design for FreeObjects involves the following steps:

  1. The reference goes out of scope. The reference here represents both the owner's python references, as well as other borrower references throughout the cluster, which is tracked through the owner reference.
  2. The owner finds out, and sends a pubsub message (initialized when pinning the primary copy) to the primary copy raylet.
  3. The primary copy raylet frees its primary copy, and then broadcasts free secondary copies to all nodes within the cluster. (This is batched and sent periodically). The reason the primary copy raylet needs to broadcast secondary copies is because it doesn't store object locations for its primary copy, this is only stored on the owner. Thus, in order to ensure all secondary copies are freed, there is a need to broadcast to all nodes.

Design

As noted above, the owner stores the locations of all copies of the objects, where pinned_at_node_id_ represents the primary copy location and the locations vector demonstrates all secondary object locations.

We now move towards a new design FreeObjects. We will get rid of the pubsub between owner -> primary copy raylet. Instead:

  1. The reference goes out of scope and marked by python.
  2. The owner sends RPCs to primary and secondary copy holders to free their objects. (Since it has all of its locations)

However, this doesn't easily solve our issue since the pubsub also has an owner_dead_callback, which fires when the owner has died. This will help clean up the primary object and ensure there are no memory leaks in this case. To continue supporting this functionality, if the owner dies, this information is propagated by the GCS, and thus each raylet just listens to the GCS events and updates their states accordingly.

Description

This PR covers the first part of the FreeObjects change. This first part is strictly adding functionality for the owner to be in charge of freeing objects. We make the first step away from this design such that the owner calls a new FreeLocalObjects RPC to all primary and secondary copy locations when OnObjectsOutOfScopeOrFreed.

Considerations

NodeManager RPC vs. ObjectManager RPC

For this PR, I considered either 1) Adding a new RPC to NodeManager or 2) Adding new route that calls into existing ObjectManager FreeObjects. Ultimately, I decided to go with 1). A design consideration here is that workers currently only call into raylets' NodeManager, so part of this is to keep with existing implementation. Another reason is that our new HandleFreeLocalObjects would be responsible for 2 things: 1) if primary copy, delete metadata from LocalObjectManager (manages spilled and primary copies) and 2) if any copy, queue up within FreeObjects. Thus, NodeManager currently has functionality that calls into LocalObjectManager. Furthermore, I find that directly interfacing with ObjectManager is not as intuitive, and creating a new path just for ObjectManager does not seem ideal.

Moving Around ReferenceCounter Variables

For this PR, I decided to follow similar formats in the codebase, and have all the lambdas within the constructor shifted to the end. In this case, they can be considered optional arguments and can be defaulted to null if not specified. This leads to easier testing implementations. A tricky part is to ensure these functions generate no-op if they are not specified.

Only Owner Frees

An important consideration is that now that we are sending from the worker side, only if the object is freed is owned by the current worker should the worker send RPC requests to the object locations. Otherwise, the worker should just keep quiet.

Dead Code Deleted in Future

For this PR, I kept FreeObjects's local_only=false case still alive, even though this case is essentially never called again. In the future, I will delete this dead code.

Testing Changes and ReferenceCounter Changes

For this PR, there were many testing changes that are needed because of this new path that is different from the first one. For now, many of these tests are just hardcoded to match correct behavior, but in the future some unnecessary tests might be deleted or shuffled around to match the new implementation.

Testing

Ran python tests and C++ tests adjacent to these changes to ensure no regressions. Future e2e testing in #63181

Introduces Race Condition

Note that this PR actually introduces a race condition. Since this is currently still a half step to removing the pubsub, there is a race in the following example.

  • Two nodes, where one is owner and the other is the primary copy holder
  • Let us say refcount hits 0 on the owner, we then queue up FreeLocalObjects
  • In our current case, we also fire the subscription pubsub as a no-op to the primary copy holder
  • The pubsub connection gets discontinued
  • If owner dies before sending FreeLocalObjects, then there is memory leak.

We will fix this race condition in the immediate next PR, which uses the GCS to broadcast deaths and have the raylets free accordingly.

@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 introduces a new FreeLocalObjects RPC mechanism that allows object owners to explicitly broadcast deletion requests to all raylets holding copies, moving away from the previous pub-sub based eviction. The changes include a new internal documentation file for the placement group lifecycle, updates to the ReferenceCounter to trigger broadcasts when objects are freed, and the implementation of the RPC handler within the NodeManager. Feedback focuses on improving the robustness and efficiency of the new path, specifically by adding a retry limit to the exponential backoff logic, batching broadcast RPCs to reduce overhead, and addressing potential object leaks resulting from the partial removal of the legacy batching logic in LocalObjectManager.

Comment thread src/ray/core_worker/core_worker.cc Resolved
Comment thread src/ray/core_worker/core_worker.cc Resolved
Comment thread src/ray/raylet/local_object_manager.cc Resolved
@aaronscalene
aaronscalene force-pushed the aaron/free-objects-objm-fix branch 3 times, most recently from 9a872b8 to 0408656 Compare May 8, 2026 03:06
@aaronscalene
aaronscalene force-pushed the aaron/free-objects-objm-fix branch from 0408656 to 4f1ef12 Compare May 15, 2026 21:17
Signed-off-by: aaron.li <[email protected]>
Signed-off-by: aaron.li <[email protected]>
@aaronscalene
aaronscalene marked this pull request as ready for review May 21, 2026 03:14
@aaronscalene
aaronscalene requested a review from a team as a code owner May 21, 2026 03:14
Comment thread src/ray/protobuf/node_manager.proto Resolved
@ray-gardener ray-gardener Bot added the core Issues that should be addressed in Ray Core label May 21, 2026

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

bit of a nit for the PR description, but it sounds like a stream of consciousness right now.
Ideally someone who has no idea what FreeObjects is should be able to:
1.) Understand the problem from reading your PR description
2.) Understand the alternate solutions you proposed and why you selected this one

There's a couple places where it should go into a bit more detail. For example,

The reference goes out of scope and marked by python.

The reference count is not just the python reference count, it also includes the task submission + the number of borrowers. Collectively you can just refer to this as the reference count, but it's not just modified by python's gc but by Ray as well, key difference.

You should also mention why we spray FreeObjects to every single node in the cluster on the raylet. In that the raylet doesn't always subscribe for object locations, hence at the time it receives the pubsub from the owner that the object has gone out of scope it has no idea what the object locations are.

Comment thread src/ray/core_worker/core_worker.cc Outdated

void CoreWorker::SpreadFreeLocalObjects(const ObjectID &object_id,
const std::vector<NodeID> &locations) {
io_service_.post(

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.

why do we need to explicitly post onto the io_service here, is this potentially called on the task_execution_io_service?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We need io_service here because it is called under the ReferenceCounter mutex, which can lead to deadlock in the future. I added a comment to better explain this.

@Sparks0219 Sparks0219 May 26, 2026 •

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.

Sorry I don't follow, isn't this just making a couple RPCs? What would trigger the deadlock/what other lock is getting acquired?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sorry, you are 100% right to pushback, I was under an old impression of my implementation. Based on the previous benchmark, SpreadFreeLocalObjects runs on an average 0.08 ms, so it seems alright to just leave this within the thread.

Comment thread src/ray/core_worker/core_worker.cc Resolved
Comment thread src/ray/core_worker/core_worker.cc Resolved
Comment thread src/ray/protobuf/node_manager.proto Resolved
Comment thread src/ray/raylet/local_object_manager.cc Resolved
Comment thread src/ray/raylet/local_object_manager.h Resolved
Signed-off-by: aaron.li <[email protected]>
Comment thread src/ray/raylet/node_manager.cc Resolved
Signed-off-by: aaron.li <[email protected]>
v
Signed-off-by: aaron.li <[email protected]>
Comment thread src/ray/raylet/node_manager.cc Resolved
Signed-off-by: aaron.li <[email protected]>
Comment thread src/ray/raylet/node_manager.cc Resolved
i
Signed-off-by: aaron.li <[email protected]>
Signed-off-by: aaron.li <[email protected]>
Comment thread src/ray/raylet/local_object_manager.cc Resolved
@aaronscalene

Copy link
Copy Markdown
Contributor Author

bit of a nit for the PR description, but it sounds like a stream of consciousness right now. Ideally someone who has no idea what FreeObjects is should be able to: 1.) Understand the problem from reading your PR description 2.) Understand the alternate solutions you proposed and why you selected this one

There's a couple places where it should go into a bit more detail. For example,

The reference goes out of scope and marked by python.
The reference count is not just the python reference count, it also includes the task submission + the number of borrowers. Collectively you can just refer to this as the reference count, but it's not just modified by python's gc but by Ray as well, key difference.

You should also mention why we spray FreeObjects to every single node in the cluster on the raylet. In that the raylet doesn't always subscribe for object locations, hence at the time it receives the pubsub from the owner that the object has gone out of scope it has no idea what the object locations are.

done

@aaronscalene aaronscalene left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

reviewed

Comment thread src/ray/core_worker/reference_counter.h Outdated
bool lineage_pinning_enabled = false,
std::function<bool(const NodeID &node_id)> is_node_dead = nullptr,
std::function<void(const ObjectID &object_id, const std::vector<NodeID> &locations)>
spread_free_local_objects = nullptr)

@Sparks0219 Sparks0219 May 26, 2026 •

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.

Why should is_node_dead or spread_free_local_objects ever be nullptr? Default parameters should only exist if they make sense, I feel like here it doesn't make sense.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree with this, changed accordingly.

publisher_.get(),
subscriber_.get(),
/*is_node_dead=*/[](const NodeID &) { return false; },
fake_owned_object_count_gauge_,

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.

can you revert the parameter ordering change since we decided to keep the is_node_dead callback?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

}
if (!locations_set.empty()) {
std::vector<NodeID> locations(locations_set.begin(), locations_set.end());
spread_free_local_objects_(it->first, locations);

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.

std::move(locations)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I pass locations by reference, so this should not help? I think std::move would incur additional overhead stack cost as opposed to just passing by reference, albeit negligible.

// Decrement counter for old state
UpdateOwnedObjectCounters(object_id, it->second, /*decrement=*/true);
if (!is_node_dead_(node_id)) {
if (!is_node_dead_ || !is_node_dead_(node_id)) {

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: we really should just remove the default parameter for !is_node_dead_ just makes the code confusing

// up the local_objects_ entry once the spilled copy has been
// freed.
spilled_object_pending_delete_.push(object_id);
if (it != local_objects_.end() && !it->second.is_freed_) {

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.

why'd you just remove the early return and nest everything? I think it hurts readability and there's not much point to it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Originally, only primary copies need to fall through this path to be freed, while secondary objects are freed through HandleFreeObjects. Now, we want primary / spilled objects to have their metadata cleared, while secondary objects should still be queued up to be deleted. Thus, if it is a primary copy, we want to free the primary copy objects. Otherwise, secondary copies are queued up to be deleted.

@Sparks0219

Copy link
Copy Markdown
Contributor

Looking good! Can you collapse this PR with 2/n as well and start running it through CI?
Also think you missed what I mentioned about the ref count here #63117 (review)

Signed-off-by: aaron.li <[email protected]>

@aaronscalene aaronscalene left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

reviewed

Comment thread src/ray/core_worker/core_worker.cc Outdated

void CoreWorker::SpreadFreeLocalObjects(const ObjectID &object_id,
const std::vector<NodeID> &locations) {
io_service_.post(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sorry, you are 100% right to pushback, I was under an old impression of my implementation. Based on the previous benchmark, SpreadFreeLocalObjects runs on an average 0.08 ms, so it seems alright to just leave this within the thread.

// up the local_objects_ entry once the spilled copy has been
// freed.
spilled_object_pending_delete_.push(object_id);
if (it != local_objects_.end() && !it->second.is_freed_) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Originally, only primary copies need to fall through this path to be freed, while secondary objects are freed through HandleFreeObjects. Now, we want primary / spilled objects to have their metadata cleared, while secondary objects should still be queued up to be deleted. Thus, if it is a primary copy, we want to free the primary copy objects. Otherwise, secondary copies are queued up to be deleted.

publisher_.get(),
subscriber_.get(),
/*is_node_dead=*/[](const NodeID &) { return false; },
fake_owned_object_count_gauge_,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

}
if (!locations_set.empty()) {
std::vector<NodeID> locations(locations_set.begin(), locations_set.end());
spread_free_local_objects_(it->first, locations);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I pass locations by reference, so this should not help? I think std::move would incur additional overhead stack cost as opposed to just passing by reference, albeit negligible.

Comment thread src/ray/core_worker/reference_counter.h Outdated
bool lineage_pinning_enabled = false,
std::function<bool(const NodeID &node_id)> is_node_dead = nullptr,
std::function<void(const ObjectID &object_id, const std::vector<NodeID> &locations)>
spread_free_local_objects = nullptr)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree with this, changed accordingly.

l
Signed-off-by: aaron.li <[email protected]>

@aaronscalene aaronscalene left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

reviewed with PR

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit e308c88. Configure here.

if (!locations_set.empty()) {
std::vector<NodeID> locations(locations_set.begin(), locations_set.end());
spread_free_local_objects_(it->first, locations);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

External I/O performed while holding ReferenceCounter mutex

Medium Severity

spread_free_local_objects_ is invoked inside OnObjectOutOfScopeOrFreed, which requires mutex_ to be held exclusively. The callback resolves into CoreWorker::SpreadFreeLocalObjects, which calls GetNodeAddressAndLiveness (acquires node_cache_address_and_liveness_mutex_), GetOrConnectByAddress (may create gRPC channels), and sends RPCs — all while blocking every other thread that needs the ReferenceCounter mutex. This creates lock-ordering risks and can stall reference-counting operations for the duration of multiple network lookups.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit e308c88. Configure here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Should be fine, as mentioned in comment above that the time for SpreadFreeLocalObjects seemed minimal overall in benchmarking.

@aaronscalene aaronscalene changed the title [core] (FreeObjects 1/n) Integrated new NodeManager FreeLocalObjects RPC [core] [DO NOT MERGE] (FreeObjects 1/n) Integrated new NodeManager FreeLocalObjects RPC May 30, 2026
@aaronscalene aaronscalene changed the title [core] [DO NOT MERGE] (FreeObjects 1/n) Integrated new NodeManager FreeLocalObjects RPC [core] [DO NOT MERGE] (FreeObjects) Integrated new NodeManager FreeLocalObjects RPC May 30, 2026
@aaronscalene aaronscalene changed the title [core] [DO NOT MERGE] (FreeObjects) Integrated new NodeManager FreeLocalObjects RPC [core] [DO NOT MERGE] (FreeObjects 1/n) Integrated new NodeManager FreeLocalObjects RPC May 30, 2026
edoakes pushed a commit that referenced this pull request Jun 9, 2026
… free mechanism (#63218)

## Motivation
Within this multi-step PR process, our goal is to change our current
**FreeObjects** implementation to an owner driven process. Current
design of FreeObjects involves spraying the entire cluster with
FreeObjects RPCs. This introduces many caveats: bloating observability
with FreeObjects RPC counts, sending excessive RPCs, and worse mental
model from owner-driven reference counting. This is a continuation from
#63117. Due to the race condition detailed in the testing section of the
PR, we decided to merge both changes for a complete PR.

Our current design for **FreeObjects** involves the following steps:
1. The reference goes out of scope. The reference here represents both
the owner's python references, as well as other borrower references
throughout the cluster, which is tracked through the owner reference.
2. The owner finds out, and sends a pubsub message (initialized when
pinning the primary copy) to the primary copy raylet.
3. The primary copy raylet frees its primary copy, and then broadcasts
free secondary copies to all nodes within the cluster. (This is batched
and sent periodically). The reason the primary copy raylet needs to
broadcast secondary copies is because it doesn't store object locations
for its primary copy, this is only stored on the owner. Thus, in order
to ensure all secondary copies are freed, there is a need to broadcast
to all nodes.

## Design
As noted above, the owner stores the locations of all copies of the
objects, where `pinned_at_node_id_` represents the primary copy location
and the `locations` vector demonstrates all secondary object locations.

We now move towards a new design **FreeObjects**. We will get rid of the
pubsub between owner -> primary copy raylet. Instead:
1. The reference goes out of scope. The reference here represents both
the owner's python references, as well as other borrower references
throughout the cluster, which is tracked through the owner reference.
2. The owner sends RPCs to primary and secondary copy holders to free
their objects. (Since it has all of its locations)

However, this doesn't easily solve our issue since the pubsub also has
an `owner_dead_callback`, which fires when the owner has died. This will
help clean up the primary object and ensure there are no memory leaks in
this case. To continue supporting this functionality, if the owner dies,
this information is propagated by the GCS, and thus each raylet just
listens to the GCS events and updates their states accordingly.

Originally, the design was that:
- the pubsub connection fails / ends for some unexpected reason
- the owner is thought to be dead, so the primary copy raylet then frees
its primary copy and queues up **FreeObjects** to broadcast to all
secondary copies

The new design:
- The GCS already publishes WorkerFailure and NodeRemoved announcements
to all raylets. This is so that raylets know to release worker leases of
dead nodes / workers
- With this new design, the raylets will listen and additionally clear
any objects that had their owner on the failed worker / node
- Something to consider is how exactly GCS gets these notifications, and
if there would be any slowness in these notifications.

1.) When the worker process dies (but the node remains alive), the
raylet picks up the worker death relatively quickly via the broken IPC
connection and sends the worker death up to the GCS, and GCS broadcasts
it.

2.) On node death, similar story.

However, in old implementation, the long poll connection would be
sitting the retryable grpc client queue waiting for either the
connection to come back up or the GCS to get a worker death
notification. Hence all in all, there really shouldn't be a significant
change in behavior.

## Description
This PR covers the first and second part of the **FreeObjects** change.
The first part is strictly adding functionality for the owner to be in
charge of freeing objects. We make the first step away from this design
such that the owner calls a new **FreeLocalObjects** RPC to all primary
and secondary copy locations when **OnObjectsOutOfScopeOrFreed**. The
second part amends the functionality of the current owner dead pubsub
callback to be moved from an eager pubsub death callback to a more
passive listener callback.

## Considerations
### NodeManager RPC vs. ObjectManager RPC
For this PR, I considered either 1) Adding a new RPC to **NodeManager**
or 2) Adding new route that calls into existing **ObjectManager**
FreeObjects. Ultimately, I decided to go with 1). A design consideration
here is that workers currently only call into raylets' **NodeManager**,
so part of this is to keep with existing implementation. Another reason
is that our new **HandleFreeLocalObjects** would be responsible for 2
things: 1) if primary copy, delete metadata from **LocalObjectManager**
(manages spilled and primary copies) and 2) if any copy, queue up within
**FreeObjects**. Thus, **NodeManager** currently has functionality that
calls into **LocalObjectManager**. Furthermore, I find that directly
interfacing with **ObjectManager** is not as intuitive, and creating a
new path just for **ObjectManager** does not seem ideal.

### Moving Around ReferenceCounter Variables
For this PR, I decided to follow similar formats in the codebase, and
have all the lambdas within the constructor shifted to the end. In this
case, they can be considered optional arguments and can be defaulted to
null if not specified. This leads to easier testing implementations. A
tricky part is to ensure these functions generate no-op if they are not
specified.

### Only Owner Frees
An important consideration is that now that we are sending from the
worker side, only if the object is freed is owned by the current worker
should the worker send RPC requests to the object locations. Otherwise,
the worker should just keep quiet.

### Dead Code Deleted in Future
For this PR, I kept **FreeObjects**'s `local_only=false` case still
alive, even though this case is essentially never called again. In the
future, I will delete this dead code.

### Testing Changes and ReferenceCounter Changes
For this PR, there were many testing changes that are needed because of
this new path that is different from the first one. For now, many of
these tests are just hardcoded to match correct behavior, but in the
future some unnecessary tests might be deleted or shuffled around to
match the new implementation.

### Pass Over All Objects
This new design introduces a pass over all objects within the current
node every announcement. However, failed nodes and worker announcements
should not be too common and if the object is not owned by the failed
worker / node it would be a no-op; thus this should not lead to any
latency issues.

### Owner is Less Eager
This new design will be a less eager free, especially for the primary
copy of the node. This is definitely an important part to consider,
however since it is similar a pubsub for node information, the latency
should not be that big of a deal. Furthermore, the pubsub interface
already keeps track of network failures etc. to some extent, and if
there are further failures / GCS failures there are probably worse
problems to deal with.

### LocalObjectManager local_objects_ vs. ObjectManager local_objects_
An important distinction is needed between both classes' local objects,
where we need to aggregate and free any objects within both classes.
LocalObjectManager in this case keeps track of primary copies as well as
spilled copies. ObjectManager keeps track of anything in plasma such as
primary copies / secondary copies. Thus, we need to get the union of
both objects to get the objects that we want to free.

## Testing
Ran python tests and C++ tests adjacent to these changes to ensure no
regressions. Future e2e testing in #63181

We will fix this race condition in the immediate next PR, which uses the
GCS to broadcast deaths and have the raylets free accordingly.

---------

Signed-off-by: aaron.li <[email protected]>
Signed-off-by: aaronscalene <[email protected]>
@aaronscalene aaronscalene changed the title [core] [DO NOT MERGE] (FreeObjects 1/n) Integrated new NodeManager FreeLocalObjects RPC [core] [DO NOT MERGE] (Original FreeObjects) Integrated new NodeManager FreeLocalObjects RPC Jun 10, 2026
sampan-s-nayak pushed a commit to sampan-s-nayak/ray that referenced this pull request Jun 10, 2026
… free mechanism (ray-project#63218)

## Motivation
Within this multi-step PR process, our goal is to change our current
**FreeObjects** implementation to an owner driven process. Current
design of FreeObjects involves spraying the entire cluster with
FreeObjects RPCs. This introduces many caveats: bloating observability
with FreeObjects RPC counts, sending excessive RPCs, and worse mental
model from owner-driven reference counting. This is a continuation from
ray-project#63117. Due to the race condition detailed in the testing section of the
PR, we decided to merge both changes for a complete PR.

Our current design for **FreeObjects** involves the following steps:
1. The reference goes out of scope. The reference here represents both
the owner's python references, as well as other borrower references
throughout the cluster, which is tracked through the owner reference.
2. The owner finds out, and sends a pubsub message (initialized when
pinning the primary copy) to the primary copy raylet.
3. The primary copy raylet frees its primary copy, and then broadcasts
free secondary copies to all nodes within the cluster. (This is batched
and sent periodically). The reason the primary copy raylet needs to
broadcast secondary copies is because it doesn't store object locations
for its primary copy, this is only stored on the owner. Thus, in order
to ensure all secondary copies are freed, there is a need to broadcast
to all nodes.

## Design
As noted above, the owner stores the locations of all copies of the
objects, where `pinned_at_node_id_` represents the primary copy location
and the `locations` vector demonstrates all secondary object locations.

We now move towards a new design **FreeObjects**. We will get rid of the
pubsub between owner -> primary copy raylet. Instead:
1. The reference goes out of scope. The reference here represents both
the owner's python references, as well as other borrower references
throughout the cluster, which is tracked through the owner reference.
2. The owner sends RPCs to primary and secondary copy holders to free
their objects. (Since it has all of its locations)

However, this doesn't easily solve our issue since the pubsub also has
an `owner_dead_callback`, which fires when the owner has died. This will
help clean up the primary object and ensure there are no memory leaks in
this case. To continue supporting this functionality, if the owner dies,
this information is propagated by the GCS, and thus each raylet just
listens to the GCS events and updates their states accordingly.

Originally, the design was that:
- the pubsub connection fails / ends for some unexpected reason
- the owner is thought to be dead, so the primary copy raylet then frees
its primary copy and queues up **FreeObjects** to broadcast to all
secondary copies

The new design:
- The GCS already publishes WorkerFailure and NodeRemoved announcements
to all raylets. This is so that raylets know to release worker leases of
dead nodes / workers
- With this new design, the raylets will listen and additionally clear
any objects that had their owner on the failed worker / node
- Something to consider is how exactly GCS gets these notifications, and
if there would be any slowness in these notifications.

1.) When the worker process dies (but the node remains alive), the
raylet picks up the worker death relatively quickly via the broken IPC
connection and sends the worker death up to the GCS, and GCS broadcasts
it.

2.) On node death, similar story.

However, in old implementation, the long poll connection would be
sitting the retryable grpc client queue waiting for either the
connection to come back up or the GCS to get a worker death
notification. Hence all in all, there really shouldn't be a significant
change in behavior.

## Description
This PR covers the first and second part of the **FreeObjects** change.
The first part is strictly adding functionality for the owner to be in
charge of freeing objects. We make the first step away from this design
such that the owner calls a new **FreeLocalObjects** RPC to all primary
and secondary copy locations when **OnObjectsOutOfScopeOrFreed**. The
second part amends the functionality of the current owner dead pubsub
callback to be moved from an eager pubsub death callback to a more
passive listener callback.

## Considerations
### NodeManager RPC vs. ObjectManager RPC
For this PR, I considered either 1) Adding a new RPC to **NodeManager**
or 2) Adding new route that calls into existing **ObjectManager**
FreeObjects. Ultimately, I decided to go with 1). A design consideration
here is that workers currently only call into raylets' **NodeManager**,
so part of this is to keep with existing implementation. Another reason
is that our new **HandleFreeLocalObjects** would be responsible for 2
things: 1) if primary copy, delete metadata from **LocalObjectManager**
(manages spilled and primary copies) and 2) if any copy, queue up within
**FreeObjects**. Thus, **NodeManager** currently has functionality that
calls into **LocalObjectManager**. Furthermore, I find that directly
interfacing with **ObjectManager** is not as intuitive, and creating a
new path just for **ObjectManager** does not seem ideal.

### Moving Around ReferenceCounter Variables
For this PR, I decided to follow similar formats in the codebase, and
have all the lambdas within the constructor shifted to the end. In this
case, they can be considered optional arguments and can be defaulted to
null if not specified. This leads to easier testing implementations. A
tricky part is to ensure these functions generate no-op if they are not
specified.

### Only Owner Frees
An important consideration is that now that we are sending from the
worker side, only if the object is freed is owned by the current worker
should the worker send RPC requests to the object locations. Otherwise,
the worker should just keep quiet.

### Dead Code Deleted in Future
For this PR, I kept **FreeObjects**'s `local_only=false` case still
alive, even though this case is essentially never called again. In the
future, I will delete this dead code.

### Testing Changes and ReferenceCounter Changes
For this PR, there were many testing changes that are needed because of
this new path that is different from the first one. For now, many of
these tests are just hardcoded to match correct behavior, but in the
future some unnecessary tests might be deleted or shuffled around to
match the new implementation.

### Pass Over All Objects
This new design introduces a pass over all objects within the current
node every announcement. However, failed nodes and worker announcements
should not be too common and if the object is not owned by the failed
worker / node it would be a no-op; thus this should not lead to any
latency issues.

### Owner is Less Eager
This new design will be a less eager free, especially for the primary
copy of the node. This is definitely an important part to consider,
however since it is similar a pubsub for node information, the latency
should not be that big of a deal. Furthermore, the pubsub interface
already keeps track of network failures etc. to some extent, and if
there are further failures / GCS failures there are probably worse
problems to deal with.

### LocalObjectManager local_objects_ vs. ObjectManager local_objects_
An important distinction is needed between both classes' local objects,
where we need to aggregate and free any objects within both classes.
LocalObjectManager in this case keeps track of primary copies as well as
spilled copies. ObjectManager keeps track of anything in plasma such as
primary copies / secondary copies. Thus, we need to get the union of
both objects to get the objects that we want to free.

## Testing
Ran python tests and C++ tests adjacent to these changes to ensure no
regressions. Future e2e testing in ray-project#63181

We will fix this race condition in the immediate next PR, which uses the
GCS to broadcast deaths and have the raylets free accordingly.

---------

Signed-off-by: aaron.li <[email protected]>
Signed-off-by: aaronscalene <[email protected]>
limarkdcunha pushed a commit to limarkdcunha/ray that referenced this pull request Jun 30, 2026
… free mechanism (ray-project#63218)

## Motivation
Within this multi-step PR process, our goal is to change our current
**FreeObjects** implementation to an owner driven process. Current
design of FreeObjects involves spraying the entire cluster with
FreeObjects RPCs. This introduces many caveats: bloating observability
with FreeObjects RPC counts, sending excessive RPCs, and worse mental
model from owner-driven reference counting. This is a continuation from
ray-project#63117. Due to the race condition detailed in the testing section of the
PR, we decided to merge both changes for a complete PR.

Our current design for **FreeObjects** involves the following steps:
1. The reference goes out of scope. The reference here represents both
the owner's python references, as well as other borrower references
throughout the cluster, which is tracked through the owner reference.
2. The owner finds out, and sends a pubsub message (initialized when
pinning the primary copy) to the primary copy raylet.
3. The primary copy raylet frees its primary copy, and then broadcasts
free secondary copies to all nodes within the cluster. (This is batched
and sent periodically). The reason the primary copy raylet needs to
broadcast secondary copies is because it doesn't store object locations
for its primary copy, this is only stored on the owner. Thus, in order
to ensure all secondary copies are freed, there is a need to broadcast
to all nodes.

## Design
As noted above, the owner stores the locations of all copies of the
objects, where `pinned_at_node_id_` represents the primary copy location
and the `locations` vector demonstrates all secondary object locations.

We now move towards a new design **FreeObjects**. We will get rid of the
pubsub between owner -> primary copy raylet. Instead:
1. The reference goes out of scope. The reference here represents both
the owner's python references, as well as other borrower references
throughout the cluster, which is tracked through the owner reference.
2. The owner sends RPCs to primary and secondary copy holders to free
their objects. (Since it has all of its locations)

However, this doesn't easily solve our issue since the pubsub also has
an `owner_dead_callback`, which fires when the owner has died. This will
help clean up the primary object and ensure there are no memory leaks in
this case. To continue supporting this functionality, if the owner dies,
this information is propagated by the GCS, and thus each raylet just
listens to the GCS events and updates their states accordingly.

Originally, the design was that:
- the pubsub connection fails / ends for some unexpected reason
- the owner is thought to be dead, so the primary copy raylet then frees
its primary copy and queues up **FreeObjects** to broadcast to all
secondary copies

The new design:
- The GCS already publishes WorkerFailure and NodeRemoved announcements
to all raylets. This is so that raylets know to release worker leases of
dead nodes / workers
- With this new design, the raylets will listen and additionally clear
any objects that had their owner on the failed worker / node
- Something to consider is how exactly GCS gets these notifications, and
if there would be any slowness in these notifications.

1.) When the worker process dies (but the node remains alive), the
raylet picks up the worker death relatively quickly via the broken IPC
connection and sends the worker death up to the GCS, and GCS broadcasts
it.

2.) On node death, similar story.

However, in old implementation, the long poll connection would be
sitting the retryable grpc client queue waiting for either the
connection to come back up or the GCS to get a worker death
notification. Hence all in all, there really shouldn't be a significant
change in behavior.

## Description
This PR covers the first and second part of the **FreeObjects** change.
The first part is strictly adding functionality for the owner to be in
charge of freeing objects. We make the first step away from this design
such that the owner calls a new **FreeLocalObjects** RPC to all primary
and secondary copy locations when **OnObjectsOutOfScopeOrFreed**. The
second part amends the functionality of the current owner dead pubsub
callback to be moved from an eager pubsub death callback to a more
passive listener callback.

## Considerations
### NodeManager RPC vs. ObjectManager RPC
For this PR, I considered either 1) Adding a new RPC to **NodeManager**
or 2) Adding new route that calls into existing **ObjectManager**
FreeObjects. Ultimately, I decided to go with 1). A design consideration
here is that workers currently only call into raylets' **NodeManager**,
so part of this is to keep with existing implementation. Another reason
is that our new **HandleFreeLocalObjects** would be responsible for 2
things: 1) if primary copy, delete metadata from **LocalObjectManager**
(manages spilled and primary copies) and 2) if any copy, queue up within
**FreeObjects**. Thus, **NodeManager** currently has functionality that
calls into **LocalObjectManager**. Furthermore, I find that directly
interfacing with **ObjectManager** is not as intuitive, and creating a
new path just for **ObjectManager** does not seem ideal.

### Moving Around ReferenceCounter Variables
For this PR, I decided to follow similar formats in the codebase, and
have all the lambdas within the constructor shifted to the end. In this
case, they can be considered optional arguments and can be defaulted to
null if not specified. This leads to easier testing implementations. A
tricky part is to ensure these functions generate no-op if they are not
specified.

### Only Owner Frees
An important consideration is that now that we are sending from the
worker side, only if the object is freed is owned by the current worker
should the worker send RPC requests to the object locations. Otherwise,
the worker should just keep quiet.

### Dead Code Deleted in Future
For this PR, I kept **FreeObjects**'s `local_only=false` case still
alive, even though this case is essentially never called again. In the
future, I will delete this dead code.

### Testing Changes and ReferenceCounter Changes
For this PR, there were many testing changes that are needed because of
this new path that is different from the first one. For now, many of
these tests are just hardcoded to match correct behavior, but in the
future some unnecessary tests might be deleted or shuffled around to
match the new implementation.

### Pass Over All Objects
This new design introduces a pass over all objects within the current
node every announcement. However, failed nodes and worker announcements
should not be too common and if the object is not owned by the failed
worker / node it would be a no-op; thus this should not lead to any
latency issues.

### Owner is Less Eager
This new design will be a less eager free, especially for the primary
copy of the node. This is definitely an important part to consider,
however since it is similar a pubsub for node information, the latency
should not be that big of a deal. Furthermore, the pubsub interface
already keeps track of network failures etc. to some extent, and if
there are further failures / GCS failures there are probably worse
problems to deal with.

### LocalObjectManager local_objects_ vs. ObjectManager local_objects_
An important distinction is needed between both classes' local objects,
where we need to aggregate and free any objects within both classes.
LocalObjectManager in this case keeps track of primary copies as well as
spilled copies. ObjectManager keeps track of anything in plasma such as
primary copies / secondary copies. Thus, we need to get the union of
both objects to get the objects that we want to free.

## Testing
Ran python tests and C++ tests adjacent to these changes to ensure no
regressions. Future e2e testing in ray-project#63181

We will fix this race condition in the immediate next PR, which uses the
GCS to broadcast deaths and have the raylets free accordingly.

---------

Signed-off-by: aaron.li <[email protected]>
Signed-off-by: aaronscalene <[email protected]>
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
… free mechanism (ray-project#63218)

## Motivation
Within this multi-step PR process, our goal is to change our current
**FreeObjects** implementation to an owner driven process. Current
design of FreeObjects involves spraying the entire cluster with
FreeObjects RPCs. This introduces many caveats: bloating observability
with FreeObjects RPC counts, sending excessive RPCs, and worse mental
model from owner-driven reference counting. This is a continuation from
ray-project#63117. Due to the race condition detailed in the testing section of the
PR, we decided to merge both changes for a complete PR.

Our current design for **FreeObjects** involves the following steps:
1. The reference goes out of scope. The reference here represents both
the owner's python references, as well as other borrower references
throughout the cluster, which is tracked through the owner reference.
2. The owner finds out, and sends a pubsub message (initialized when
pinning the primary copy) to the primary copy raylet.
3. The primary copy raylet frees its primary copy, and then broadcasts
free secondary copies to all nodes within the cluster. (This is batched
and sent periodically). The reason the primary copy raylet needs to
broadcast secondary copies is because it doesn't store object locations
for its primary copy, this is only stored on the owner. Thus, in order
to ensure all secondary copies are freed, there is a need to broadcast
to all nodes.

## Design
As noted above, the owner stores the locations of all copies of the
objects, where `pinned_at_node_id_` represents the primary copy location
and the `locations` vector demonstrates all secondary object locations.

We now move towards a new design **FreeObjects**. We will get rid of the
pubsub between owner -> primary copy raylet. Instead:
1. The reference goes out of scope. The reference here represents both
the owner's python references, as well as other borrower references
throughout the cluster, which is tracked through the owner reference.
2. The owner sends RPCs to primary and secondary copy holders to free
their objects. (Since it has all of its locations)

However, this doesn't easily solve our issue since the pubsub also has
an `owner_dead_callback`, which fires when the owner has died. This will
help clean up the primary object and ensure there are no memory leaks in
this case. To continue supporting this functionality, if the owner dies,
this information is propagated by the GCS, and thus each raylet just
listens to the GCS events and updates their states accordingly.

Originally, the design was that:
- the pubsub connection fails / ends for some unexpected reason
- the owner is thought to be dead, so the primary copy raylet then frees
its primary copy and queues up **FreeObjects** to broadcast to all
secondary copies

The new design:
- The GCS already publishes WorkerFailure and NodeRemoved announcements
to all raylets. This is so that raylets know to release worker leases of
dead nodes / workers
- With this new design, the raylets will listen and additionally clear
any objects that had their owner on the failed worker / node
- Something to consider is how exactly GCS gets these notifications, and
if there would be any slowness in these notifications.

1.) When the worker process dies (but the node remains alive), the
raylet picks up the worker death relatively quickly via the broken IPC
connection and sends the worker death up to the GCS, and GCS broadcasts
it.

2.) On node death, similar story.

However, in old implementation, the long poll connection would be
sitting the retryable grpc client queue waiting for either the
connection to come back up or the GCS to get a worker death
notification. Hence all in all, there really shouldn't be a significant
change in behavior.

## Description
This PR covers the first and second part of the **FreeObjects** change.
The first part is strictly adding functionality for the owner to be in
charge of freeing objects. We make the first step away from this design
such that the owner calls a new **FreeLocalObjects** RPC to all primary
and secondary copy locations when **OnObjectsOutOfScopeOrFreed**. The
second part amends the functionality of the current owner dead pubsub
callback to be moved from an eager pubsub death callback to a more
passive listener callback.

## Considerations
### NodeManager RPC vs. ObjectManager RPC
For this PR, I considered either 1) Adding a new RPC to **NodeManager**
or 2) Adding new route that calls into existing **ObjectManager**
FreeObjects. Ultimately, I decided to go with 1). A design consideration
here is that workers currently only call into raylets' **NodeManager**,
so part of this is to keep with existing implementation. Another reason
is that our new **HandleFreeLocalObjects** would be responsible for 2
things: 1) if primary copy, delete metadata from **LocalObjectManager**
(manages spilled and primary copies) and 2) if any copy, queue up within
**FreeObjects**. Thus, **NodeManager** currently has functionality that
calls into **LocalObjectManager**. Furthermore, I find that directly
interfacing with **ObjectManager** is not as intuitive, and creating a
new path just for **ObjectManager** does not seem ideal.

### Moving Around ReferenceCounter Variables
For this PR, I decided to follow similar formats in the codebase, and
have all the lambdas within the constructor shifted to the end. In this
case, they can be considered optional arguments and can be defaulted to
null if not specified. This leads to easier testing implementations. A
tricky part is to ensure these functions generate no-op if they are not
specified.

### Only Owner Frees
An important consideration is that now that we are sending from the
worker side, only if the object is freed is owned by the current worker
should the worker send RPC requests to the object locations. Otherwise,
the worker should just keep quiet.

### Dead Code Deleted in Future
For this PR, I kept **FreeObjects**'s `local_only=false` case still
alive, even though this case is essentially never called again. In the
future, I will delete this dead code.

### Testing Changes and ReferenceCounter Changes
For this PR, there were many testing changes that are needed because of
this new path that is different from the first one. For now, many of
these tests are just hardcoded to match correct behavior, but in the
future some unnecessary tests might be deleted or shuffled around to
match the new implementation.

### Pass Over All Objects
This new design introduces a pass over all objects within the current
node every announcement. However, failed nodes and worker announcements
should not be too common and if the object is not owned by the failed
worker / node it would be a no-op; thus this should not lead to any
latency issues.

### Owner is Less Eager
This new design will be a less eager free, especially for the primary
copy of the node. This is definitely an important part to consider,
however since it is similar a pubsub for node information, the latency
should not be that big of a deal. Furthermore, the pubsub interface
already keeps track of network failures etc. to some extent, and if
there are further failures / GCS failures there are probably worse
problems to deal with.

### LocalObjectManager local_objects_ vs. ObjectManager local_objects_
An important distinction is needed between both classes' local objects,
where we need to aggregate and free any objects within both classes.
LocalObjectManager in this case keeps track of primary copies as well as
spilled copies. ObjectManager keeps track of anything in plasma such as
primary copies / secondary copies. Thus, we need to get the union of
both objects to get the objects that we want to free.

## Testing
Ran python tests and C++ tests adjacent to these changes to ensure no
regressions. Future e2e testing in ray-project#63181

We will fix this race condition in the immediate next PR, which uses the
GCS to broadcast deaths and have the raylets free accordingly.

---------

Signed-off-by: aaron.li <[email protected]>
Signed-off-by: aaronscalene <[email protected]>
Signed-off-by: 400Ping <[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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants