# Exception: ray.init() called, but ray client is already connected

**URL:** <https://discuss.ray.io/t/exception-ray-init-called-but-ray-client-is-already-connected/7239>\
**Category:** Ray Serve\
**Created:** [August 18, 2022, 9:27am UTC](https://discuss.ray.io/t/exception-ray-init-called-but-ray-client-is-already-connected/7239 "2022-08-18T09:27:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hyrmec](https://avatars.discourse-cdn.com/v4/letter/h/c0e974/32.png) [@hyrmec](https://discuss.ray.io/u/hyrmec)\
**Post date:** [August 18, 2022, 9:27am UTC](https://discuss.ray.io/t/exception-ray-init-called-but-ray-client-is-already-connected/7239/1 "2022-08-18T09:27:39Z")

</div>

**How severe does this issue affect your experience of using Ray?**

- High: It blocks me to complete my task.

This error occurs when trying to deploy a ray-serve deployment to a ray cluster using (  
ray.init(  
address=‘ray://127.0.0.1:10001’,  
namespace=“serve”,  
runtime\_env=runtime\_env,  
ignore\_reinit\_error=True  
)

If I run one node locally and set  
ray.init(  
address=‘auto’,  
namespace=“serve”,  
runtime\_env=runtime\_env,  
ignore\_reinit\_error=True  
)  
ray serve deployment works fine.

trace:  
Traceback (most recent call last):  
File “deploy.py”, line 50, in   
ignore\_reinit\_error=True  
File “/home/pnikulshin/projects/serve\_robots/face\_control/venv/lib/python3.7/site-packages/ray/util/client/ **init**.py”, line 282, in init  
ret = self.get\_context().init(\*args, \*\*kwargs)  
File “/home/pnikulshin/projects/serve\_robots/face\_control/venv/lib/python3.7/site-packages/ray/util/client/ **init**.py”, line 195, in init  
self.connect(“127.0.0.1:50051”)  
File “/home/pnikulshin/projects/serve\_robots/face\_control/venv/lib/python3.7/site-packages/ray/util/client/ **init**.py”, line 68, in connect  
raise Exception(“ray.init() called, but ray client is already connected”)  
Exception: ray.init() called, but ray client is already connected

Try to ray-1.13.0 and ray-2.0.0rc1, python3.7.7

---

<div class="post-metadata">

**Author:** ![architkulkarni](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/architkulkarni/32/10_2.png) [@architkulkarni](https://discuss.ray.io/u/architkulkarni)\
**Post date:** [August 18, 2022, 4:51pm UTC](https://discuss.ray.io/t/exception-ray-init-called-but-ray-client-is-already-connected/7239/2 "2022-08-18T16:51:42Z")

</div>

Hi @hyrmec , sorry you’re running into this. Could you help us out with some short self-contained reproduction steps? Is it that you’re running`ray start --head` on your local machine, and then the line

```auto
ray.init(
address=‘ray://127.0.0.1:10001’,
namespace=“serve”,
runtime_env=runtime_env,
ignore_reinit_error=True
)

```

fails with that error? Or does it fail when you deploy a Serve deployment?

---

<div class="post-metadata">

**Author:** ![hyrmec](https://avatars.discourse-cdn.com/v4/letter/h/c0e974/32.png) [@hyrmec](https://discuss.ray.io/u/hyrmec)\
**Post date:** [August 19, 2022, 6:37am UTC](https://discuss.ray.io/t/exception-ray-init-called-but-ray-client-is-already-connected/7239/3 "2022-08-19T06:37:28Z")

</div>

I have a cluster running in k8s, I have forwarded the ray ports to myself locally. I’m trying to deploy my ray-serve deployment to the cluster, which is well deployed locally.  
I am using RuntimeEnv to deliver my code and environment to the cluster.

My code:  
from fastapi import Request, Response, FastAPI, HTTPException  
from ray import serve  
from ray.util.client import ray  
from ray.runtime\_env import RuntimeEnv

app = FastAPI()  
runtime\_env = RuntimeEnv(  
working\_dir=‘https://path\_to\_my\_repo.zip’,  
pip=‘./requirements.txt’  
)  
ray.init(  
#address=‘auto’,  
address=‘ray://127.0.0.1:10001’,  
namespace=“serve”,  
runtime\_env=runtime\_env,  
ignore\_reinit\_error=True  
)  
serve.start(detached=True)

@serve.deployment()  
@serve.ingress(app)  
class FaceControl:  
def **init** (self):  
self.face\_recognizer = ‘my-robot’

```
@app.get("/compare_faces")
def compare_faces(self, main_url: str, target_url: str):
    return True

```

FaceControl.deploy()

I execute python deploy.py  
and get Exception: ray.init() called, but ray client is already connected

---

<div class="post-metadata">

**Author:** ![hyrmec](https://avatars.discourse-cdn.com/v4/letter/h/c0e974/32.png) [@hyrmec](https://discuss.ray.io/u/hyrmec)\
**Post date:** [August 19, 2022, 6:43am UTC](https://discuss.ray.io/t/exception-ray-init-called-but-ray-client-is-already-connected/7239/4 "2022-08-19T06:43:27Z")

</div>

Tell me how you deploy the ray serve code to the ray cluster

---

<div class="post-metadata">

**Author:** ![architkulkarni](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/architkulkarni/32/10_2.png) [@architkulkarni](https://discuss.ray.io/u/architkulkarni)\
**Post date:** [August 19, 2022, 5:00pm UTC](https://discuss.ray.io/t/exception-ray-init-called-but-ray-client-is-already-connected/7239/5 "2022-08-19T17:00:58Z")

</div>

Thanks for the further details! Let me try to reproduce this; I don’t see anything wrong with the example you posted. In the meantime, you can try calling `ray.shutdown()` on the line before `ray.init()`, which may get rid of the “already connected” error, though I don’t yet understand how it could already be connected.

---

<div class="post-metadata">

**Author:** ![hyrmec](https://avatars.discourse-cdn.com/v4/letter/h/c0e974/32.png) [@hyrmec](https://discuss.ray.io/u/hyrmec)\
**Post date:** [August 25, 2022, 8:08am UTC](https://discuss.ray.io/t/exception-ray-init-called-but-ray-client-is-already-connected/7239/6 "2022-08-25T08:08:17Z")

</div>

@architkulkarni The only successful ray-serve deploy option:

kubectl exec -it head\_pod  
git clone my repo  
run deploy script

If RAY\_ADDRESS != auto then there are problems either with access to the module or with something.

---

<div class="post-metadata">

**Author:** ![architkulkarni](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/architkulkarni/32/10_2.png) [@architkulkarni](https://discuss.ray.io/u/architkulkarni)\
**Post date:** [August 25, 2022, 3:42pm UTC](https://discuss.ray.io/t/exception-ray-init-called-but-ray-client-is-already-connected/7239/7 "2022-08-25T15:42:39Z")

</div>

Glad you were able to find a workaround. Did `ray.shutdown()` not prevent the error? In general, `ray.init("ray:// ...")` should work when run locally to connect to a remote cluster.

In Ray 2.0, the recommended way to run Ray Serve in production on Kubernetes is to deploy a RayService on KubeRay: [Deploying on Kubernetes — Ray 2.0.0](https://docs.ray.io/en/releases-2.0.0/serve/production-guide/kubernetes.html). This might be helpful if you’re able to upgrade to Ray 2.0.
