Skip to content

[Core] Ensure Node._node_labels initializes regardless of connect_only#61618

Merged
edoakes merged 5 commits intoray-project:masterfrom
dancingactor:get_node_labels
Mar 12, 2026
Merged

[Core] Ensure Node._node_labels initializes regardless of connect_only#61618
edoakes merged 5 commits intoray-project:masterfrom
dancingactor:get_node_labels

Conversation

@dancingactor
Copy link
Contributor

Description

This PR ensure Node._node_labels initializes regardless of connect_only. The original code only initializesdNode._node_labels when connect_only = true

Related issues

Closes #61604

Additional information

Verification

Testing Script

import ray

ray.init()
print(ray.runtime_context.get_runtime_context().get_node_labels())

Before Fix

Traceback (most recent call last):
  File "/code/labs/jakob/ray/node_labels.py", line 7, in <module>
    print(ray.runtime_context.get_runtime_context().get_node_labels())
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/ray/runtime_context.py", line 598, in get_node_labels
    return worker.current_node_labels
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/ray/_private/worker.py", line 634, in current_node_labels
    return self.node.node_labels
           ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/ray/_private/node.py", line 685, in node_labels
    return self._node_labels
           ^^^^^^^^^^^^^^^^^
AttributeError: 'Node' object has no attribute '_node_labels'. Did you mean: 'node_labels'?

After Fix

{'ray.io/node-id': 'b32f99e4d621a79a6a001662c1e0fd54b929765b0f69bb12e263c2b7'}

Signed-off-by: dancingactor <s990346@gmail.com>
@dancingactor dancingactor requested a review from a team as a code owner March 10, 2026 07:13
Copy link
Contributor

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

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 AttributeError by ensuring the _node_labels attribute of the Node class is consistently initialized across all code paths. The security review found no vulnerabilities. The fix involves moving initialization logic out of a conditional block, and a suggestion has been made to enhance robustness against None values.

…fo['labels'] is None

Signed-off-by: dancingactor <s990346@gmail.com>
@ray-gardener ray-gardener bot added core Issues that should be addressed in Ray Core community-contribution Contributed by the community labels Mar 10, 2026
@edoakes
Copy link
Collaborator

edoakes commented Mar 10, 2026

@MengjinYan @ryanaoleary PTAL

@edoakes edoakes added the go add ONLY when ready to merge, run all tests label Mar 10, 2026
Signed-off-by: dancingactor <s990346@gmail.com>
@ryanaoleary
Copy link
Contributor

Can we add a test for the connect_only = false case?

Signed-off-by: dancingactor <s990346@gmail.com>
Signed-off-by: dancingactor <s990346@gmail.com>
@dancingactor
Copy link
Contributor Author

I noticed that _node_labels tests were missing for both connect_only = true and false scenarios, so I added unit tests for each in be4aaa4

@dancingactor
Copy link
Contributor Author

I also noticed a potential discrepancy in the label's doc, which says that

You can’t set labels using ray.init(). Local Ray clusters don’t support labels."

But we actually can set labels using ray.init(). For example

def test_ray_init_set_node_labels(shutdown_only):
labels = {"gpu_type": "A100", "region": "us"}
ray.init(labels=labels)

@edoakes edoakes merged commit 4b4ab01 into ray-project:master Mar 12, 2026
6 checks passed
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.

[Core] Exception on RuntimeContext.get_node_labels() in local cluster

4 participants