ray icon indicating copy to clipboard operation
ray copied to clipboard

[Core] `ray.get_runtime_context().namespace` returns incorrect namespace in Actor Tasks

Open woshiyyya opened this issue 1 year ago • 2 comments
trafficstars

What happened + What you expected to happen

ray.get_runtime_context().namespace always returns the default namespace, even if the Actor is launched in a custom namespace.

Versions / Dependencies

nightly

Reproduction script

import ray

@ray.remote
class Actor:
    def show_namespace(self):
        print("namespace: ", ray.get_runtime_context().namespace)

# Create an actor with specified namespace.
actor = Actor.options(
    name="my_actor", 
    namespace="actor_namespace", # <- specify namespace here
    lifetime="detached"
).remote()

ray.get(actor.show_namespace.remote())

# The Output
# >>> (Actor pid=30522) namespace:  540bbea1-bc1c-4ec3-8447-a858958151ce

It should be "actor_namespace" instead.

Issue Severity

High: It blocks me from completing my task.

woshiyyya avatar Apr 24 '24 04:04 woshiyyya

maybe it is not a bug in local mode

look at this code in ray/src/ray/core_worker/core_worker.cc image

982945902 avatar Apr 26 '24 08:04 982945902

@jjyao @kevin85421 https://github.com/ray-project/ray/pull/45025 i add a api to support this issue

982945902 avatar Apr 30 '24 03:04 982945902

ray.get_runtime_context() returns the job config for entire Ray job, not specific task/actor.

Ray does provide an API to access specific actor via ray.get_actor(<name>, <namespace>). But you have to specify namespace with that.

For this issue, it is using the API for the wrong job IMHO.

May I ask what's your use case to get the namespace inside the actor? Because in your code you can easily pass down the namespace to the actor.

hongchaodeng avatar May 01 '24 22:05 hongchaodeng

ray.get_runtime_context() returns the job config for entire Ray job, not specific task/actor.

Hmm. The RuntimeContext's constructor is initialized with a worker process. I believe that RuntimeContext is specific to each worker process (task/actor). I also performed a simple experiment. Do I miss anything?

import ray

ray.init()

@ray.remote
def f():
    runtime_context = ray.get_runtime_context()
    print("Ray task")
    print("Job", ray.get_runtime_context().get_job_id())
    print("Task", ray.get_runtime_context().get_task_id())
    print("Actor", ray.get_runtime_context().get_actor_id())

@ray.remote
class actor:
    def __init__(self):
        pass
    def print_message(self):
        runtime_context = ray.get_runtime_context()
        print("Ray actor")
        print("Job", ray.get_runtime_context().get_job_id())
        print("Task", ray.get_runtime_context().get_task_id())
        print("Actor", ray.get_runtime_context().get_actor_id())

task_obj_ref = f.remote()
ray.get(task_obj_ref)

actor_instance = actor.remote()
ray.get(actor_instance.print_message.remote())
Screenshot 2024-05-01 at 9 27 58 PM

kevin85421 avatar May 02 '24 04:05 kevin85421

Thanks for following up. As @kevin85421 said here, an api that returns the actor's namespace suffice my use case.

woshiyyya avatar May 07 '24 17:05 woshiyyya