ray
ray copied to clipboard
[Core] `ray.get_runtime_context().namespace` returns incorrect namespace in Actor Tasks
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.
maybe it is not a bug in local mode
look at this code in ray/src/ray/core_worker/core_worker.cc
@jjyao @kevin85421 https://github.com/ray-project/ray/pull/45025 i add a api to support this issue
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.
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())
Thanks for following up. As @kevin85421 said here, an api that returns the actor's namespace suffice my use case.