Skip to content

Conversation

@antfin-oss
Copy link

@antfin-oss antfin-oss commented Nov 17, 2025

This Pull Request was created automatically to merge the latest changes from fix_runtime_env_port into upstream_2.46.0 branch.

πŸ“… Created: 2025-11-17
πŸ”€ Merge direction: fix_runtime_env_port β†’ upstream_2.46.0
πŸ€– Triggered by: Manual

Please review and merge if everything looks good.

Summary by Sourcery

Merge fix_runtime_env_port branch into upstream_2.46.0 to support configurable port ranges for runtime environment.

Enhancements:

  • Introduce RAY_PORT_RANGE_LOW and RAY_PORT_RANGE_HIGH environment variables to define the port selection bounds in node._get_unused_port
  • Replace unbounded random port selection with a range-based allocation to avoid GRPC port conflicts

@sourcery-ai
Copy link

sourcery-ai bot commented Nov 17, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactors the port allocation logic in _get_unused_port to use a configurable environment-based port range rather than an arbitrary high-number range.

Class diagram for updated _get_unused_port logic in Node

classDiagram
    class Node {
        _get_unused_port(allocated_ports=None)
    }
    Node : +_get_unused_port(allocated_ports=None)
    Node : - Uses os.getenv("RAY_PORT_RANGE_LOW", 10000)
    Node : - Uses os.getenv("RAY_PORT_RANGE_HIGH", 11499)
    Node : - random.randint(low_end, high_end)
    Node : - Allocated ports checked
Loading

Flow diagram for new port allocation logic in _get_unused_port

flowchart TD
    A["Start _get_unused_port"] --> B["Get RAY_PORT_RANGE_LOW from env (default 10000)"]
    B --> C["Get RAY_PORT_RANGE_HIGH from env (default 11499)"]
    C --> D["For NUM_PORT_RETRIES times"]
    D --> E["Generate 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"]
Loading

File-Level Changes

Change Details Files
Introduce environment-configurable port range for new_port selection
  • Read RAY_PORT_RANGE_LOW and RAY_PORT_RANGE_HIGH from the environment
  • Replace random.randint(port, 65535) with random.randint(low_end, high_end)
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

@gemini-code-assist
Copy link

Summary of Changes

Hello @antfin-oss, I'm Gemini Code Assist1! 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 introduces a crucial enhancement to Ray's port allocation strategy by making the range for generating unused ports configurable through environment variables. This modification provides greater control over port selection, aiming to prevent conflicts, particularly those encountered by GRPC, and thereby improving the overall stability and reliability of the Ray runtime environment.

Highlights

  • Configurable Port Range: The mechanism for generating unused ports has been updated to allow the port range to be configured via environment variables, specifically RAY_PORT_RANGE_LOW and RAY_PORT_RANGE_HIGH.
  • Default Port Range: If not explicitly set, the configurable port range will default to 10000-11499, providing a more controlled and predictable set of ports for allocation.
  • GRPC Port Conflict Mitigation: This change aims to resolve issues where GRPC might fail due to port conflicts by providing a defined and potentially narrower range for port selection, as noted in issue [Core] Node Failure Long Running Test Failed at grpc::ServerInterface::RegisteredAsyncRequest::IssueRequest()Β ray-project/ray#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 πŸ‘ and πŸ‘Ž 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.

Footnotes

  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. ↩

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location> `python/ray/_private/node.py:979-980` </location>
<code_context>
         s.bind(("", 0))
         port = s.getsockname()[1]

+        low_end = int(os.getenv("RAY_PORT_RANGE_LOW", 10000))
+        high_end = int(os.getenv("RAY_PORT_RANGE_HIGH", 11499))
+
+
</code_context>

<issue_to_address>
**suggestion (bug_risk):** Consider validating that low_end is less than or equal to high_end.

A check for low_end > high_end would prevent ValueError from random.randint and provide a clearer error message if environment variables are misconfigured.

```suggestion
        low_end = int(os.getenv("RAY_PORT_RANGE_LOW", 10000))
        high_end = int(os.getenv("RAY_PORT_RANGE_HIGH", 11499))
        if low_end > high_end:
            raise ValueError(
                f"Invalid port range: RAY_PORT_RANGE_LOW ({low_end}) is greater than RAY_PORT_RANGE_HIGH ({high_end}). "
                "Please check your environment variable configuration."
            )
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click πŸ‘ or πŸ‘Ž on each comment and I'll use the feedback to improve your reviews.

Comment on lines +979 to +980
low_end = int(os.getenv("RAY_PORT_RANGE_LOW", 10000))
high_end = int(os.getenv("RAY_PORT_RANGE_HIGH", 11499))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Consider validating that low_end is less than or equal to high_end.

A check for low_end > high_end would prevent ValueError from random.randint and provide a clearer error message if environment variables are misconfigured.

Suggested change
low_end = int(os.getenv("RAY_PORT_RANGE_LOW", 10000))
high_end = int(os.getenv("RAY_PORT_RANGE_HIGH", 11499))
low_end = int(os.getenv("RAY_PORT_RANGE_LOW", 10000))
high_end = int(os.getenv("RAY_PORT_RANGE_HIGH", 11499))
if low_end > high_end:
raise ValueError(
f"Invalid port range: RAY_PORT_RANGE_LOW ({low_end}) is greater than RAY_PORT_RANGE_HIGH ({high_end}). "
"Please check your environment variable configuration."
)

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces configurable port ranges for the runtime environment, which is a good improvement for avoiding port conflicts. My review focuses on making the new implementation more robust by adding validation and safer type conversions for the environment variables. I've provided a single comment with a code suggestion to address these points.

Comment on lines +979 to +982
low_end = int(os.getenv("RAY_PORT_RANGE_LOW", 10000))
high_end = int(os.getenv("RAY_PORT_RANGE_HIGH", 11499))


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This implementation has a couple of potential issues:

  1. Unsafe type conversion: Using int(os.getenv(...)) can raise a ValueError if the environment variable is set to a non-integer string (e.g., an empty string), which would crash the process. It's safer to use the ray_constants.env_integer helper, which handles this gracefully.
  2. Lack of range validation: If low_end is greater than high_end, random.randint() will raise a ValueError. It's best to validate the range and handle this case, for example by swapping the values and logging a warning.

Here is a suggestion that addresses both points.

Suggested change
low_end = int(os.getenv("RAY_PORT_RANGE_LOW", 10000))
high_end = int(os.getenv("RAY_PORT_RANGE_HIGH", 11499))
low_end = ray_constants.env_integer("RAY_PORT_RANGE_LOW", 10000)
high_end = ray_constants.env_integer("RAY_PORT_RANGE_HIGH", 11499)
if low_end > high_end:
logger.warning(f"RAY_PORT_RANGE_LOW ({low_end}) > RAY_PORT_RANGE_HIGH ({high_end}). Swapping.")
low_end, high_end = high_end, low_end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants