Skip to content

[core][rdt] Support out-of-order actors by extracting metadata when creating - #59610

Merged
dayshah merged 14 commits into
ray-project:masterfrom
dayshah:native-extract-metadata
Jan 7, 2026
Merged

dayshah merged 14 commits into
ray-project:masterfrom
dayshah:native-extract-metadata

Conversation

@dayshah

@dayshah dayshah commented Dec 21, 2025 •

Copy link
Copy Markdown
Contributor

Problem

Resolves #56398

Today, RDT will launch a get_metadata task when you submit the task that creates an RDT object. This get_metadata task will run on the ray_system concurrency group until the object that it was launched for shows up in the gpu_object_store.
Out of order actors can deadlock when using RDT because the get_metadata tasks aren't ordered. Something like this could cause a deadlock:

a_ref = a.create()
b_ref = b.consume(a_ref)
a_ref2 = a.create2(b_ref)

# get_metadata task for a_ref2 starts before get_metadata task for a_ref and 
# blocks the ray_system thread forever because a_ref2 indirectly depends
# on a_ref getting transferred. a_ref can't get transferred because its get_metadata
# is stuck behind a_ref2's get_metadata

Note that there are other possible deadlock scenarios for two-sided communication, but for now we're only supporting two-sided communicators for in-order actors.

Solution

We're now creating the metadata when the object is created, pickling it, and sending it back to the owner in the PushTaskReply. The metadata is now processed on the owner in TaskManager::CompletePendingTask. From here, we'll call a callback into cython which will unpickle, get the gpu object manager, and add the tensor metadata for the object into the gpu object manager's _managed_gpu_object_metadata map.

If the metadata hasn't arrived at the time the user submits the downstream task, we'll queue the transfer instead of triggering it. When the tensor metadata arrives in the _managed_gpu_object_metadata map we'll trigger the transfer. When ray.getting the object through the tensor transport we'll first have a condition variable wait for the tensor metadata to arrive in the map. When ray.getting the RDT object through the object store, I made a change to wait_and_get_object instead of just asserting it's there and getting (it would've worked before on in-order actors because the task to get through the object store would always be queued up after the get_metadata task).

We'll do the same thing for freeing objects. If the tensor metadata hasn't arrived at the time we free, we'll queue up the free and then trigger the actual free once the tensor metadata comes in (once we know the object has actually been created). This does come with the caveat that if the object creation task doesn't successfully complete, we'll leak the object id inside this new _queued_frees set.

Now that _managed_gpu_object_metadata and _queued_transfers can be accessed from the core worker io context thread or the user python thread, I'm putting them behind a lock. They're now only accessed through accessor functions that access them under the mutex. I'm also putting gpu_object_store initialization and gc thread initialization under the same mutex in the gpu object manager.

Drawbacks

We're forcing cuda synchronization at the end of task execution for NIXL on gpu's because we're doing memory registration during ExecuteTask. This means that the actor's main thread will be blocked and will not start executing the next task (which could've been queueing up more kernels / gpu commands during this time) until the previous output has materialized. This could lead to the gpu being idle for the time between the execution of tasks, while it would've not been idle during that time before this change.

Borrowing and doing a ray.get doesn't work unless the object was created with a ray.put or the metadata arrived before the borrow. The metadata we send to the borrower might not have the tensor metadata if the owner doesn't have it at the time we serialize the ref. Today if the metadata hasn't arrived it'll be fine because we'll just send it as an object ref. We can fix this by sending the metadata in the GetObjectStatusReply which borrowers request from RegisterOwnershipInfoAndResolveFuture.

@dayshah dayshah added the go add ONLY when ready to merge, run all tests label Dec 21, 2025

@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 significant refactoring to Ray Direct Transport (RDT) to support out-of-order actor method calls. The core change involves extracting RDT metadata at object creation time and sending it to the object's owner. This allows for queuing transfers if metadata is not yet available, which is handled by a new mechanism in the GPUObjectManager using condition variables and a transfer queue. The changes span across Python, Cython, and C++ layers and appear to be consistent and well-structured. I've identified one medium-severity issue related to timeout handling that could improve error reporting.

Signed-off-by: dayshah <[email protected]>
Signed-off-by: dayshah <[email protected]>
Signed-off-by: dayshah <[email protected]>


def test_send_actor_dies(ray_start_regular):
def test_send_actor_dies_before_sending(ray_start_regular):

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'm making a follow up pr to fix when the sender dies before creating. Right now it'll result in a timeout instead of immediately realizing the upstream actor is dead. Before it relied on the get_metadata failing. Instead we should be failing the task from the C++ dependency resolver if the upstream task failed.

ray.get(result)

assert "ActorDiedError" in str(excinfo.value)
assert "nixlBackendError" in str(excinfo.value)

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.

It's a nixl error instead of actor died now because before we would fail in the recv on the ray.get(metadata_ref) and that would raise the ActorDiedError. Now we're failing when nixl tries to connect to the remote agent in the recv.

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.

Hmm can we catch the nixl error and throw ActorDiedError or at least an extra message saying that the remote actor may have died?

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 don't want to throw an ActorDiedError because it's possible it's just some nixl issue and the actor didn't actually die. But adding an rdt hint that the actor died makes sense, now I'm raising an RDT error with the hint and showing the nixl error, it looks like:

ray.exceptions.RayDirectTransportError: System error: The NIXL recv failed for object id: f4402ec78d3a260771ac7205b42681cfd83c156a0100000001000000. The source actor may have died during the transfer. The exception thrown from the nixl recv was:
 Traceback (most recent call last):
  File "/home/ubuntu/ray/python/ray/experimental/gpu_object_manager/nixl_tensor_transport.py", line 186, in recv_multiple_tensors
    remote_name = nixl_agent.add_remote_agent(remote_nixl_agent_meta)
  File "/opt/conda/envs/ray-dev/lib/python3.10/site-packages/nixl_cu12/_api.py", line 797, in add_remote_agent
    agent_name = self.agent.loadRemoteMD(metadata)
nixl_cu12._bindings.nixlBackendError: NIXL_ERR_BACKEND

@dayshah
dayshah marked this pull request as ready for review December 23, 2025 06:59
@dayshah
dayshah requested a review from a team as a code owner December 23, 2025 06:59
Comment thread src/ray/protobuf/common.proto Resolved
@ray-gardener ray-gardener Bot added core Issues that should be addressed in Ray Core gpu GPU related issues labels Dec 23, 2025
Signed-off-by: dayshah <[email protected]>
)

with self._lock:
# Since sent_dest_actors is mutable, this whole block needs to be protected.

@dayshah dayshah Dec 23, 2025 •

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.

this part is a bit funky, a couple alternatives are:

  1. Make the set a frozenset, but pay the cost of the set copy per transfer
  2. Have a separate map for the dest_actors stuff, and the map and everything in it needs to be accessed under the mutex

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 the whole block and not just the first part where we update the metadata?

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.

the entire block does things that needs the lock

  1. update dest_actors in metadata
  2. access dest actors in metadata (needs to be read under lock since dest actors is mutable)
  3. update sent_to_src_actor_and_others_warned in metadata under if condition

I release the lock after 3

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.

Oh I see, thanks!

@property
def gpu_object_store(self) -> "ray.experimental.GPUObjectStore":
with self.gpu_object_store_lock:
with self._lock:

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.

Hmm I think it is clearer to have a separate lock for singletons vs the tensor metadata. Conceptually they are responsible for different state.

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 had it because I feel like having two locks kinda makes the person reading the code do an extra bit of thinking about possible deadlocks, but ya they are conceptually different, added a separate _init_lock

I do think the thread safetiness model is getting harder to think about than it should be with a couple locks here and a thread-safe queue and an extra lock in the gpu object store. Maybe worth simplifying or protecting the gpu_object_store through the gpu_object_manager or making them one class or something

up transfers for the object.
"""
with self._tensor_transport_meta_cv:
self._managed_gpu_object_metadata[

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.

Check that the metadata is present in this dictionary before setting? I think GC could run before this method does.

@dayshah dayshah Dec 24, 2025 •

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.

ya that's true, there's an extra io context post to this after CompletePendingTask too making it more possible, checking if it exists now

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.

Actually with the new free logic, this isn't possible anymore so not expecting it to exist again now

if the tensor transport metadata is not available.
"""
with self._lock:
tensor_transport_meta = self._managed_gpu_object_metadata[

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.

Assert metadata is present.

@dayshah dayshah Dec 24, 2025 •

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.

this will raise a KeyError here anyways if not present? And it always should be since this is only called when your passing a ref to a task

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.

Prefer assertions so it's clear it's a system bug, but it's okay.

# TODO(#57962): Metadata is currently not removed on borrowers that borrow through
# the NIXL ray.put / ray.get
gpu_object_meta = self.pop_gpu_object_metadata(object_id)
if gpu_object_meta is None:

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.

When can it be None?

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.

Ya this is only called on out of scope, which is guaranteed to only happen once rn since lineage reconstruction isn't possible. Removed the None block


src_actor = ray.get_runtime_context().current_actor
self.gpu_object_store.add_object_primary(obj_ref.hex(), tensors)
tensor_transport_meta = self.gpu_object_store.add_object_primary(

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.

Where is self defined?

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.

this is in put_object on gpu_object_manager, it isn't static? Or referring to something else?

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.

? Doesn't this need to remove @staticmethod for self to be defined?

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.

oh i think the git diff makes it look weird, the @staticmethod is added on actor_has_tensor_transport but this code is inside put_object

ray.get(result)

assert "ActorDiedError" in str(excinfo.value)
assert "nixlBackendError" in str(excinfo.value)

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.

Hmm can we catch the nixl error and throw ActorDiedError or at least an extra message saying that the remote actor may have died?

Comment thread python/ray/_private/serialization.py Resolved
Comment thread src/ray/protobuf/common.proto Resolved
Signed-off-by: dayshah <[email protected]>
Signed-off-by: dayshah <[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 gpu GPU related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[core][rdt] Better support asyncio with RDT

3 participants