Skip to content

Commit fd34b15

Browse files
Add runtime env port range conf (#682)
* Add runtime env agent port range conf. * fix * Apply suggestion from @gemini-code-assist[bot] fix get env Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent c851f80 commit fd34b15

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

python/ray/_private/node.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -988,12 +988,19 @@ def _get_unused_port(self, allocated_ports=None):
988988
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
989989
s.bind(("", 0))
990990
port = s.getsockname()[1]
991+
low_end = ray_constants.env_integer("RAY_PORT_RANGE_LOW", port)
992+
high_end = ray_constants.env_integer("RAY_PORT_RANGE_HIGH", 65535)
993+
if low_end > high_end:
994+
raise ValueError(
995+
f"Invalid port range: RAY_PORT_RANGE_LOW ({low_end}) must be less than or equal to RAY_PORT_RANGE_HIGH ({high_end})."
996+
)
991997

992-
# Try to generate a port that is far above the 'next available' one.
993-
# This solves issue #8254 where GRPC fails because the port assigned
994-
# from this method has been used by a different process.
998+
# Try to generate a port that is far above the 'next available' one
999+
# or from a given range(if low_end and high_end is defined). This
1000+
# solves issue #8254 where GRPC fails because the port assigned from
1001+
# this method has been used by a different process.
9951002
for _ in range(ray_constants.NUM_PORT_RETRIES):
996-
new_port = random.randint(port, 65535)
1003+
new_port = random.randint(low_end, high_end)
9971004
if new_port in allocated_ports:
9981005
# This port is allocated for other usage already,
9991006
# so we shouldn't use it even if it's not in use right now.

0 commit comments

Comments
 (0)