ant-ray icon indicating copy to clipboard operation
ant-ray copied to clipboard

[WIP] fix runtime env agent port conflict

Open xsuler opened this issue 4 weeks ago • 2 comments

Why are these changes needed?

Related issue number

Checks

  • [ ] I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • [ ] I've run scripts/format.sh to lint the changes in this PR.
  • [ ] I've included any doc changes needed for https://docs.ray.io/en/master/.
    • [ ] I've added any new APIs to the API Reference. For example, if I added a method in Tune, I've added it in doc/source/tune/api/ under the corresponding .rst file.
  • [ ] I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
  • Testing Strategy
    • [ ] Unit tests
    • [ ] Release tests
    • [ ] This PR is not tested :(

Summary by Sourcery

Restrict the random port selection for the runtime environment agent to a configurable range to avoid port conflicts.

Bug Fixes:

  • Use RAY_PORT_RANGE_LOW and RAY_PORT_RANGE_HIGH environment variables (defaulting to 10000–11499) to bound the random port selection
  • Prevent selecting ports outside the configured range instead of using the full ephemeral port range

xsuler avatar Oct 31 '25 03:10 xsuler

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Constrain the random port allocation in the node’s _get_unused_port method to a configurable environment-based range to prevent conflicts with other processes.

Class diagram for updated _get_unused_port method in Node

classDiagram
class Node {
  +_get_unused_port(allocated_ports=None)
  +low_end : int (from env RAY_PORT_RANGE_LOW)
  +high_end : int (from env RAY_PORT_RANGE_HIGH)
  +new_port : int (random in [low_end, high_end])
}

Flow diagram for port allocation with environment-based range

flowchart TD
    A["Start _get_unused_port()"] --> B["Read RAY_PORT_RANGE_LOW and RAY_PORT_RANGE_HIGH from environment"]
    B --> C["Set low_end and high_end"]
    C --> D["For NUM_PORT_RETRIES times"]
    D --> E["Pick new_port = random.randint(low_end, high_end)"]
    E --> F["Check if new_port in allocated_ports"]
    F -->|Yes| G["Skip port"]
    F -->|No| H["Return new_port"]

File-Level Changes

Change Details Files
Introduce configurable port range for _get_unused_port
  • Parse RAY_PORT_RANGE_LOW and RAY_PORT_RANGE_HIGH from environment with defaults of 10000 and 11499
  • Replace random.randint(port, 65535) with random.randint(low_end, high_end)
  • Maintain collision check against allocated_ports
python/ray/_private/node.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an issue from a review comment by replying to it. You can also reply to a review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull request title to generate a title at any time. You can also comment @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in the pull request body to generate a PR summary at any time exactly where you want it. You can also comment @sourcery-ai summary on the pull request to (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the pull request to resolve all Sourcery comments. Useful if you've already addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull request to dismiss all existing Sourcery reviews. Especially useful if you want to start fresh with a new review - don't forget to comment @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

  • Contact our support team for questions or feedback.
  • Visit our documentation for detailed guides and information.
  • Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.

sourcery-ai[bot] avatar Oct 31 '25 03:10 sourcery-ai[bot]

Summary of Changes

Hello @xsuler, I'm Gemini Code Assist[^1]! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a recurring issue where Ray agents encounter port conflicts, particularly affecting GRPC communication, by allowing the system to select dynamic ports from a user-defined or default restricted range. This change enhances the stability and reliability of Ray's runtime environment by preventing agents from binding to ports that are already in use or fall outside acceptable operational boundaries.

Highlights

  • Configurable Port Range: Introduced environment variables RAY_PORT_RANGE_LOW (default 10000) and RAY_PORT_RANGE_HIGH (default 11499) to define a configurable range for dynamically allocated ports.
  • Port Conflict Resolution: Modified the _get_unused_port function to select a random port exclusively within the newly defined RAY_PORT_RANGE_LOW and RAY_PORT_RANGE_HIGH boundaries, directly addressing GRPC port conflicts as noted in issue #8254.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with :thumbsup: and :thumbsdown: on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

[^1]: Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

gemini-code-assist[bot] avatar Oct 31 '25 03:10 gemini-code-assist[bot]

This pull request has been automatically marked as stale because it has not had any activity for 14 days. It will be closed in another 14 days if no further activity occurs. Thank you for your contributions.

You can always ask for help on our discussion forum or Ray's public slack channel.

If you'd like to keep this open, just leave any comment, and the stale label will be removed.

github-actions[bot] avatar Nov 15 '25 01:11 github-actions[bot]

This has been merged in https://github.com/antgroup/ant-ray/pull/682

liying919 avatar Nov 20 '25 03:11 liying919