Actor creation causes serve.deployment to error with ray.init twice

Creating a ray.remote Actor along with a serve deployment binding prevents serve run from executing properly. It errors with ray.init being called twice. I can use serve build to create a configuration yaml. That configuration does allow serve deploy to work as intended but is not useful during development.

From what I can tell, the creation of the actor_handle starts up a ray cluster and that cluster is not honored with the serve deployment binding. Hence the error when attempting to create a second cluster.

A minimal script that exhibits the issue:

# deployment.py
import ray
from ray import serve

@ray.remote
class Actor:
    def __init__(self):
        ...

print("Creating actor")
actor_handle = Actor.remote()
print("After creating actor")

@serve.deployment
class SimpleDeployment:
    async def __call__(self):
        # use actor_handle here
        ...
    
graph = SimpleDeployment.bind()

Output:

% serve run deployment:graph 
2023-07-10 10:09:23,638 INFO scripts.py:380 -- Deploying from import path: "deployment:graph".
Creating actor
2023-07-10 10:09:25,702 INFO worker.py:1616 -- Started a local Ray instance. View the dashboard at 127.0.0.1:8265 
After creating actor
Traceback (most recent call last):
...
RuntimeError: Maybe you called ray.init twice by accident? This error can be suppressed by passing in 'ignore_reinit_error=True' or by calling 'ray.shutdown()' prior to 'ray.init()'.

This issue is possibly fixed by https://github.com/ray-project/ray/pull/35886 Can you try to use nightly build and see if you still got the error?

You can find nightly wheels at Installing Ray — Ray 3.0.0.dev0

Installing the 3.0.0.dev0 wheel seems to have fixed that particular issue. Thank you.

% serve run deployment:graph
2023-07-11 08:18:24,965 INFO scripts.py:407 -- Running import path: 'deployment:graph'.
Creating actor
Usage stats collection is enabled. To disable this, run the following command: `ray disable-usage-stats` before starting Ray. See https://docs.ray.io/en/master/cluster/usage-stats.html for more details.
2023-07-11 08:18:27,483 INFO worker.py:1610 -- Started a local Ray instance. View the dashboard at 127.0.0.1:8265 
After creating actor
(HTTPProxyActor pid=79971) INFO:     Started server process [79971]
(ServeController pid=79968) INFO 2023-07-11 08:18:29,857 controller 79968 deployment_state.py:1319 - Deploying new version of deployment default_SimpleDeployment.
(ServeController pid=79968) INFO 2023-07-11 08:18:29,960 controller 79968 deployment_state.py:1586 - Adding 1 replica to deployment default_SimpleDeployment.
2023-07-11 08:18:30,864 INFO router.py:908 -- Using PowerOfTwoChoicesReplicaScheduler.
2023-07-11 08:18:30,872 INFO router.py:370 -- Got updated replicas for deployment default_SimpleDeployment: {'default_SimpleDeployment#zJpbqa'}.
2023-07-11 08:18:30,873 SUCC scripts.py:448 -- Deployed Serve app successfully.