Skip to content

[core] support ipv6 in host network mode - #54662

Closed
Basasuya wants to merge 6 commits into
ray-project:masterfrom
Basasuya:basasuya_master_support_ipv6_patch
Closed

Basasuya wants to merge 6 commits into
ray-project:masterfrom
Basasuya:basasuya_master_support_ipv6_patch

Conversation

@Basasuya

Copy link
Copy Markdown
Contributor

Why are these changes needed?

Some previous issues also describe requirements for the use of IPv6. I think IPv6 mode is important feature for Ray. Many online environments have IPv6 network cards, and ray could not running on machines which only support IPv6 cards.

In our enviroment, we are running ray in ipv6 host network mode kubernetes pod.

Related issue number

#54660

Checks

@Basasuya
Basasuya requested review from a team as code owners July 16, 2025 12:41

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Summary of Changes

Hello @Basasuya, 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 comprehensive support for IPv6 networking within Ray, enabling it to operate seamlessly in environments that primarily use IPv6, such as Kubernetes pods configured with host network mode. The changes involve updating core networking logic across various Ray components to correctly handle IPv6 addresses, improving address parsing, and introducing environment variables for explicit IPv6 preference and configuration.

Highlights

  • Core IPv6 Support: Enabled various Ray components, including the runtime environment agent, dashboard agents, GRPC servers, and the remote PDB, to correctly bind to and communicate using IPv6 addresses. This is primarily controlled by the RAY_PREFER_IPV6 environment variable.
  • Robust IP/Port Parsing: Updated numerous instances of IP address and port parsing logic across the codebase (both Python and C++) from split(':') to rsplit(':', 1) or a custom SplitIpPort utility. This ensures correct separation of the IP address from the port, especially for IPv6 addresses which can contain multiple colons (e.g., [::1]:8000).
  • IPv6 Environment Variable Integration: Introduced and utilized RAY_PREFER_IPV6 and RAY_POD_IPV6 environment variables to signal IPv6 preference and provide the specific pod's IPv6 address. This allows Ray to adapt its networking behavior accordingly, particularly for localhost resolution and external connectivity checks in IPv6 environments.
  • New Utility Functions: Added a new is_ipv6_address helper function in Python (python/ray/_private/utils.py) and a SplitIpPort utility in C++ (src/ray/util/util.h) to reliably identify and parse IPv6 addresses, including handling bracket notation for IPv6 literals.
  • Torch Distributed and PDB Compatibility: Adjusted Torch distributed training setup to strip [] from IPv6 master addresses for compatibility. Similarly, the remote PDB (rpdb) was updated to correctly handle IPv6 addresses for both listening and connecting sockets.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

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 issue 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 is currently in preview and 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 to provide feedback.

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

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

The code changes introduce IPv6 support to Ray. I've identified a few issues, including a critical logic error and a bug in a new C++ utility function, that should be addressed.

Comment thread python/ray/scripts/scripts.py Resolved
Comment thread src/ray/util/util.h
Comment on lines +64 to +75
inline std::vector<std::string> SplitIpPort(const std::string &address) {
std::vector<std::string> result;
size_t found;
found = address.find_last_of(":");
if (found == std::string::npos) {
return result;
}

result.push_back(address.substr(0, found));
result.push_back(address.substr(found + 1));
return result;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The SplitIpPort implementation incorrectly parses bare IPv6 addresses. The current implementation uses find_last_of(":") to split the address and port. This works for IPv4 but fails for bare IPv6 addresses (e.g., ::1), which it would incorrectly split into :: and 1. A more robust implementation should correctly handle different address formats, including bracketed IPv6 addresses. It will return an empty vector for bare IPv6 addresses, causing the existing RAY_CHECKs to fail, which is safer than mis-parsing the address.

inline std::vector<std::string> SplitIpPort(const std::string &address) {
  std::vector<std::string> result;
  if (address.empty()) {
    return result;
  }

  // Handle IPv6 with brackets, e.g., [::1]:8080
  if (address[0] == '[') {
    size_t r_bracket_pos = address.rfind(']');
    if (r_bracket_pos == std::string::npos) {
      // Malformed address.
      return result;
    }
    // Check for port after brackets.
    if (r_bracket_pos + 1 < address.size() && address[r_bracket_pos + 1] == ':') {
      result.push_back(address.substr(0, r_bracket_pos + 1));
      result.push_back(address.substr(r_bracket_pos + 2));
      return result;
    }
    // No port found.
    return result;
  }

  // Handle IPv4 and bare IPv6.
  size_t last_colon_pos = address.rfind(':');
  if (last_colon_pos == std::string::npos) {
    // No colon, no port.
    return result;
  }

  // If there are multiple colons, it's a bare IPv6 address. Assume no port.
  if (address.find(':') != last_colon_pos) {
    return result;
  }

  // Only one colon. Assume it's a port separator for IPv4/hostname.
  result.push_back(address.substr(0, last_colon_pos));
  result.push_back(address.substr(last_colon_pos + 1));
  return result;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a valid concern.

Comment thread python/ray/_private/services.py Outdated
Comment on lines 610 to 616
my_pod_ip6 = os.environ.get("RAY_POD_IPV6")
if my_pod_ip6 is not None and my_pod_ip6 != "":
if len(address_parts) > 1:
return ip_address + ":" + os.environ.get("RAY_GCS_SPECIFIC_PORT", "6379")
else:
return ip_address
return ":".join([ip_address] + address_parts[1:])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The condition my_pod_ip6 is not None and my_pod_ip6 != "" can be simplified to just if my_pod_ip6:1. Additionally, the use of os.environ.get("RAY_GCS_SPECIFIC_PORT", "6379") hardcodes a default port for GCS. This could be brittle if the GCS server is configured to run on a different port. Consider making this port resolution more robust instead of relying on a hardcoded default.

Style Guide References

Suggested change
my_pod_ip6 = os.environ.get("RAY_POD_IPV6")
if my_pod_ip6 is not None and my_pod_ip6 != "":
if len(address_parts) > 1:
return ip_address + ":" + os.environ.get("RAY_GCS_SPECIFIC_PORT", "6379")
else:
return ip_address
return ":".join([ip_address] + address_parts[1:])
my_pod_ip6 = os.environ.get("RAY_POD_IPV6")
if my_pod_ip6:
if len(address_parts) > 1:
# TODO(dev): Consider making this port resolution more robust
# instead of relying on a hardcoded default.
return ip_address + ":" + os.environ.get("RAY_GCS_SPECIFIC_PORT", "6379")
else:
return ip_address

Footnotes

  1. A non-empty string is truthy in Python. There is no need to compare to None or "". (link) ↩

@cszhu cszhu added community-contribution Contributed by the community core Issues that should be addressed in Ray Core labels Jul 16, 2025

@codope codope left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@Basasuya Thanks for upstreaming the changes. For hostNetwork mode only, this addresses a real need. Meanwhile, I've left a few comments to tighten up correctness. Looking forward to getting this landed!

Comment thread python/ray/scripts/scripts.py Resolved
Comment thread python/ray/_raylet.pyx Outdated
# SubscriberID / UniqueID, which is 28 (kUniqueIDSize) random bytes.
subscriber_id = bytes(bytearray(random.getrandbits(8) for _ in range(28)))
gcs_address, gcs_port = address.split(":")
gcs_address, gcs_port = address.rsplit(":", 1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If address is "[::1]:6379", gcs_address remains "[::1]". The C++ subscriber likely expects a bare IP without brackets. I think you need to strip the brackets [/] before passing into the C++ API (or preferably use a helper that handles both cases, and the helper method can be unit tested).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed

Comment thread python/ray/_private/utils.py Outdated
return in_test


def is_ipv6_address(ip):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This method strips leading [] but won’t accept link-local addresses with a zone ID (e.g. "fe80::1%eth0"). If some environment use such addresses, you’ll need to extend the util to handle %-notation correctly.

Also, this method looks like a good candidate to move to python/ray/_common instead of _private. Let's also add comprehensive unit tests for this method in python/ray/_common/tests/test_utils.py

Comment thread python/ray/util/rpdb.py Outdated
Opens a remote PDB on first available port.
"""
if debugger_external:
if os.environ.get("RAY_PREFER_IPV6") is not None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I noticed some inconsistent checks. For example, here we check for the env variable but never verify its value. And at some places like in http_server_agent.py, we also check for value. Consider keeping the checks consistent. Another suggestion is to check for os.environ.get("RAY_PREFER_IPV6", "").lower().

}
RAY_CHECK_EQ(parts.size(), 3u);
std::vector<std::string> ip_port = absl::StrSplit(parts[2], ":");
std::vector<std::string> ip_port = SplitIpPort(parts[2]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like this still assumes IPv4 addresses or use split(':'). Without corresponding changes, services started in pure-IPv6 mode may fail to bind or mis-advertise their ports?

Comment on lines +66 to +68
std::string server_address(
(listen_to_localhost_only_ ? "127.0.0.1:" : (use_ipv6 ? "[::]:" : "0.0.0.0:")) +
std::to_string(port_));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Swapping "127.0.0.1" -> "::" without also binding "::1" (loopback) may expose the Dashboard, PDB, or control plane APIs on the host’s external IPv6 interface. Best practice is to bind local-only services to ::1 explicitly, and wildcard services (cluster-in traffic) to ::.

"""
ip_address, port = address.split(":")
ip_address, port = address.rsplit(":", 1)
if ip_address == "8.8.8.8":

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why hard coded? Also, this breaks in IPv6-only environments

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ok i get it this is for DNS assuming 8.8.8.8 is always available. Is there a better way to resolve DNS?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please let's also add unit tests for this method to ensure it covers both IPv4 and IPv6 IPs.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What if the server is not connected to the Internet? We might want to allow some env var for specifying a test DNS.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This part use RAY_POD_IPV6 env to get the real node ip address which user set from ray start --node-ip-address=xxxxx.

That is to say, users use env to configure ip instead of obtaining it through DNS.

Comment thread src/ray/util/util.h
Comment on lines +64 to +75
inline std::vector<std::string> SplitIpPort(const std::string &address) {
std::vector<std::string> result;
size_t found;
found = address.find_last_of(":");
if (found == std::string::npos) {
return result;
}

result.push_back(address.substr(0, found));
result.push_back(address.substr(found + 1));
return result;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a valid concern.

"""
ip_address, port = address.split(":")
ip_address, port = address.rsplit(":", 1)
if ip_address == "8.8.8.8":

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please let's also add unit tests for this method to ensure it covers both IPv4 and IPv6 IPs.

"127.0.0.1" if args.node_ip_address == "127.0.0.1" else "0.0.0.0"
)

if os.environ.get("RAY_PREFER_IPV6") is not None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

On further discussion with @jjyao , this additional env variable might not be needed as node_ip_address will always be set. If the user sets node_ip_address explicitly then we infer whether that's an IPv4 or IPv6 address and bind to the user-specified port. All we need is a utility to tell whether the address is IPv4 and IPv6. If user does not explicitly set node_ip_address, then node_ip_address_from_perspective will deduce some ip address and then we call same utility as above.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That is a good idea, this is the solution we could remove env RAY_PREFER_IPV6

@@ -221,6 +221,9 @@ def parent_dead_callback(msg):
runtime_env_agent_ip = (
"127.0.0.1" if args.node_ip_address == "127.0.0.1" else "0.0.0.0"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Currently we always bind to 0.0.0.0 unless user explicitly specify the node ip to be localhost. Let's use this opportunity to change to ONLY bind to the node ip address user specified via ray start --node-ip-address since all the clients will only connect to it via this ip and it's more secure.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Since the pr is aiming at supporting ipv6, do we want to bind to "::" if the node_ip_address is ipv6?

@jjyao jjyao added the P0 Issues that should be fixed in short order label Jul 18, 2025
@jjyao jjyao added the go add ONLY when ready to merge, run all tests label Jul 18, 2025
Signed-off-by: Jiajun Yao <[email protected]>
@jjyao

jjyao commented Jul 24, 2025

Copy link
Copy Markdown
Contributor

@Basasuya @codope I pushed a commit so that if an variable is for storing ip only, we won't have [] in it. If a variable is for storing address (i.e. ip:port) then we will have [] if it's ipv6.

@Basasuya

Copy link
Copy Markdown
Contributor Author

I fix some bug when using ray job submit, watch ray logs after remove [] from node_ip_address

some TODO:

  1. add UT for python/ray/_private/network_utils.py and SplitIpPort(C++) and solve all UT
  2. need E2E UT for ipv6 mode?
  3. need to consider the issue of ipv6 localhost (::1)?

welcome to add other point which I have not mentioned
@jjyao @codope

@jjyao

jjyao commented Jul 29, 2025

Copy link
Copy Markdown
Contributor

For easy review, we can break this PR into a few smaller ones.

I think the first one can be creating two utils: one is combing ip and port to address, the other is split address to ip and port. And replace all the places in the codebase with these two utils. I can find someone to work on it.

@aslonnie aslonnie left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(will let @jjyao and core team to review. let me know if anything needs my input or action.)

@github-actions

Copy link
Copy Markdown

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 github-actions Bot added the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Aug 21, 2025
@github-actions

github-actions Bot commented Sep 4, 2025

Copy link
Copy Markdown

This pull request has been automatically closed because there has been no more activity in the 14 days
since being marked stale.

Please feel free to reopen or open a new pull request if you'd still like this to be addressed.

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

Thanks again for your contribution!

@github-actions github-actions Bot closed this Sep 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests P0 Issues that should be fixed in short order stale The issue is stale. It will be closed within 7 days unless there are further conversation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants