# The Ray agent couldn't be started due to the port conflict. To solve the problem, start Ray with a hard-coded agent port

**URL:** <https://discuss.ray.io/t/the-ray-agent-couldnt-be-started-due-to-the-port-conflict-to-solve-the-problem-start-ray-with-a-hard-coded-agent-port/8072>\
**Category:** Uncategorized\
**Created:** [October 28, 2022, 10:01pm UTC](https://discuss.ray.io/t/the-ray-agent-couldnt-be-started-due-to-the-port-conflict-to-solve-the-problem-start-ray-with-a-hard-coded-agent-port/8072 "2022-10-28T22:01:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![raytune\_kuberay\_user](https://avatars.discourse-cdn.com/v4/letter/r/a87d85/32.png) [@raytune\_kuberay\_user](https://discuss.ray.io/u/raytune_kuberay_user)\
**Post date:** [October 28, 2022, 10:01pm UTC](https://discuss.ray.io/t/the-ray-agent-couldnt-be-started-due-to-the-port-conflict-to-solve-the-problem-start-ray-with-a-hard-coded-agent-port/8072/1 "2022-10-28T22:01:55Z")

</div>

We are running some ray.tune unit tests on Linux. The test setup is like this. We only launch one ray cluster.

```auto

ray.init()

tune.run(xxxx)

ray.shutdown()

```

we occasionally see this error.

```auto

E ray.exceptions.RuntimeEnvSetupError: Failed to setup runtime environment.

E Could not create the actor because its associated runtime env failed to be created.

E Failed to create runtime environment {"envVars": {"TUNE_ORIG_WORKING_DIR": "xxxx”}} because the Ray agent couldn't be started due to the port conflict. See `dashboard_agent.log` for more details. To solve the problem, start Ray with a hard-coded agent port. `ray start --dashboard-agent-grpc-port [port]` and make sure the port is not used by other processes.

```

However, I think internally ray assigns this port randomly. Saw a similar issue from [TEST: CI: flaky ray windows failure: "System error: Unknown error" · Issue #4905 · modin-project/modin · GitHub](https://github.com/modin-project/modin/issues/4905) . Also, ray.init() does not have a argument called dashboard-agent-grpc-port…  
How to properly bypass this flakiness? Should we have a subprocess running `ray start --dashboard-agent-grpc-port 0`? Any suggestions?

---

<div class="post-metadata">

**Author:** ![cade](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/cade/32/3837_2.png) [@cade](https://discuss.ray.io/u/cade)\
**Post date:** [November 1, 2022, 2:17am UTC](https://discuss.ray.io/t/the-ray-agent-couldnt-be-started-due-to-the-port-conflict-to-solve-the-problem-start-ray-with-a-hard-coded-agent-port/8072/2 "2022-11-01T02:17:30Z")

</div>

cc @architkulkarni I think we should retry if we find that the port number that we randomly generated is currently in-use. Is that work planned for runtime env creation // is my diagnosis correct?

---

<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:** [November 1, 2022, 9:12pm UTC](https://discuss.ray.io/t/the-ray-agent-couldnt-be-started-due-to-the-port-conflict-to-solve-the-problem-start-ray-with-a-hard-coded-agent-port/8072/3 "2022-11-01T21:12:39Z")

</div>

Sorry you’re running into this, I think the best thing would be to specify the port in `ray start` as you suggest. We plan to fix this in the future.

---

<div class="post-metadata">

**Author:** ![raytune\_kuberay\_user](https://avatars.discourse-cdn.com/v4/letter/r/a87d85/32.png) [@raytune\_kuberay\_user](https://discuss.ray.io/u/raytune_kuberay_user)\
**Post date:** [November 4, 2022, 9:15pm UTC](https://discuss.ray.io/t/the-ray-agent-couldnt-be-started-due-to-the-port-conflict-to-solve-the-problem-start-ray-with-a-hard-coded-agent-port/8072/4 "2022-11-04T21:15:59Z")

</div>

doing

```auto
+ sock = socket.socket()
+ sock.bind(('', 0))
+ free_port = sock.getsockname()[1]
+ subprocess.check_call(["ray", "start" , "--head", f"--dashboard-agent-grpc-port={free_port}", "--include-dashboard=False"])
+ ray.init(address="auto")

```

Still doesn’t solve the issue.@architkulkarni Do you have some suggestions?

---

<div class="post-metadata">

**Author:** ![sangcho](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sangcho/32/425_2.png) [@sangcho](https://discuss.ray.io/u/sangcho)\
**Post date:** [November 9, 2022, 11:06pm UTC](https://discuss.ray.io/t/the-ray-agent-couldnt-be-started-due-to-the-port-conflict-to-solve-the-problem-start-ray-with-a-hard-coded-agent-port/8072/5 "2022-11-09T23:06:14Z")

</div>

Can you share the log from `dashboard_agent.log`?

---

<div class="post-metadata">

**Author:** ![sangcho](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sangcho/32/425_2.png) [@sangcho](https://discuss.ray.io/u/sangcho)\
**Post date:** [November 10, 2022, 5:56am UTC](https://discuss.ray.io/t/the-ray-agent-couldnt-be-started-due-to-the-port-conflict-to-solve-the-problem-start-ray-with-a-hard-coded-agent-port/8072/6 "2022-11-10T05:56:33Z")

</div>

Synced offline. @raytune_kuberay_user will try setting both agent ports specified in this doc [Configuring Ray — Ray 3.0.0.dev0](https://docs.ray.io/en/master/ray-core/configure.html#ports-configurations)

---

<div class="post-metadata">

**Author:** ![raytune\_kuberay\_user](https://avatars.discourse-cdn.com/v4/letter/r/a87d85/32.png) [@raytune\_kuberay\_user](https://discuss.ray.io/u/raytune_kuberay_user)\
**Post date:** [November 10, 2022, 5:59pm UTC](https://discuss.ray.io/t/the-ray-agent-couldnt-be-started-due-to-the-port-conflict-to-solve-the-problem-start-ray-with-a-hard-coded-agent-port/8072/7 "2022-11-10T17:59:22Z")

</div>

Thanks. For local testing, do you recommand us to do  
ray.init(local\_mode=True) ? Will this help resolving the port conflict? @sangcho

---

<div class="post-metadata">

**Author:** ![sangcho](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sangcho/32/425_2.png) [@sangcho](https://discuss.ray.io/u/sangcho)\
**Post date:** [November 10, 2022, 11:35pm UTC](https://discuss.ray.io/t/the-ray-agent-couldnt-be-started-due-to-the-port-conflict-to-solve-the-problem-start-ray-with-a-hard-coded-agent-port/8072/8 "2022-11-10T23:35:32Z")

</div>

We deprecated that feature from 2.2 (the feature was unmaintained for a while…). We recommend you to run ray.init() and run workloads. If you think local\_mode=True is important for unit test, please file a feature request to re-enable it!

---

<div class="post-metadata">

**Author:** ![nateyoder](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/nateyoder/32/4706_2.png) [@nateyoder](https://discuss.ray.io/u/nateyoder)\
**Post date:** [August 30, 2023, 11:12pm UTC](https://discuss.ray.io/t/the-ray-agent-couldnt-be-started-due-to-the-port-conflict-to-solve-the-problem-start-ray-with-a-hard-coded-agent-port/8072/9 "2023-08-30T23:12:23Z")

</div>

I’m running into the same issue (`Failed to create runtime environment for job 01000000 because the Ray agent couldn't be started due to the port conflict. See `dashboard\_agent.log` for more details.`. Is this planned way to do this from ray.init or do I still need to call out to a seperate subprocess even for unit testing? Is there now a better way to do this? Furthermore, in my ray.init call I have set `include_dashboard=False` but am still getting this error. Is this expected?
