Skip to content

[4/n] IPv6 support: Add IPv6 support for sockets - #56147

Merged
jjyao merged 33 commits into
ray-project:masterfrom
Yicheng-Lu-llll:support-ipv6-for-socket
Oct 29, 2025
Merged

jjyao merged 33 commits into
ray-project:masterfrom
Yicheng-Lu-llll:support-ipv6-for-socket

Conversation

@Yicheng-Lu-llll

@Yicheng-Lu-llll Yicheng-Lu-llll commented Sep 2, 2025 •

Copy link
Copy Markdown
Member

Why are these changes needed?

This PR updates all socket-related code to ensure compatibility with both IPv4 and IPv6.

Related issue number

Checks

See all the tests I have done here: https://docs.google.com/document/d/129aZnlNu4U3KaJbbriLHvUizUaZDnx9WgdNh50tp-c8/edit?tab=t.0


Note

Introduces IPv6-aware networking (address parsing, IP detection, socket binding, and port checks) across C++ and Python, updating services and helpers to prefer correct AF and exposing new utilities.

  • Networking Utilities (C++)
    • Add IsIPv6, GetNodeIpAddressFromPerspective, and CheckPortFree(family, port) in ray/util/network_util.{h,cc}; keep BuildAddress/ParseAddress and URL helpers; extend tests.
    • Update build to include gflags; migrate C++ callers to new IP detection and port-check APIs (raylet WorkerPool, tests, redis async context, runtime init paths).
  • Python Networking Helpers
    • Expose C++ functions via Cython (node_ip_address_from_perspective, is_ipv6), add get_localhost_ip, and adjust is_localhost.
    • Replace legacy IP detection and use IPv6-aware logic in services (get_node_ip_address, dashboard bind checks, TLS cert SANs).
  • Socket Binding & Port Utilities
    • Bind sockets with AF_INET6 when host is IPv6 across Serve HTTP, client proxier, rpdb, node startup, Spark utils, AIR/train/collective utilities; make find_free_port accept family.
  • Raylet/Workers
    • Track node address family and use family-aware CheckPortFree; improve port selection robustness.
  • Docs/Examples
    • Simplify LM training address to tcp://{ip}:{port} replacing build_address.

Written by Cursor Bugbot for commit 3e235d8. This will update automatically on new commits. Configure here.

@Yicheng-Lu-llll
Yicheng-Lu-llll force-pushed the support-ipv6-for-socket branch 2 times, most recently from a54b8e2 to 5cb3e54 Compare September 3, 2025 19:36
@@ -31,7 +32,7 @@ NativeRayRuntime::NativeRayRuntime() {

auto bootstrap_address = ConfigInternal::Instance().bootstrap_ip;
if (bootstrap_address.empty()) {
bootstrap_address = GetNodeIpAddress();
bootstrap_address = ray::GetNodeIpAddressFromPerspective();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I renamed the function to GetNodeIpAddressFromPerspective to keep it consistent with the Python function node_ip_address_from_perspective. Additionally, I used CPython bindings to avoid maintaining duplicate implementations in both C++ and Python.

Comment thread src/ray/util/network_util.cc Resolved
Comment thread src/ray/util/network_util.h Resolved
Comment thread python/ray/_private/node.py Resolved
@Yicheng-Lu-llll
Yicheng-Lu-llll force-pushed the support-ipv6-for-socket branch 2 times, most recently from fa61584 to 697488d Compare September 4, 2025 15:41
Comment thread python/ray/_common/network_utils.py Resolved
@Yicheng-Lu-llll
Yicheng-Lu-llll marked this pull request as ready for review September 4, 2025 16:21
@ray-gardener ray-gardener Bot added core Issues that should be addressed in Ray Core community-contribution Contributed by the community labels Sep 4, 2025
Comment thread python/ray/_common/network_utils.py Resolved

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

(for core team to review)

@Yicheng-Lu-llll
Yicheng-Lu-llll force-pushed the support-ipv6-for-socket branch 2 times, most recently from 21b3c6b to ccc093d Compare September 12, 2025 02:55

@jjyao jjyao 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.

Sorry for the late review.

Comment thread src/ray/util/network_util.cc Outdated
@@ -203,4 +226,91 @@ std::shared_ptr<absl::flat_hash_map<std::string, std::string>> ParseURL(std::str
return result;
}

std::string GetNodeIpAddressFromPerspective(const std::string &address) {

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.

prefer to use std::optional instead of empty string since it's more explicit.

Comment thread src/ray/util/network_util.cc Outdated
std::string GetNodeIpAddressFromPerspective(const std::string &address) {
static const auto detect_ip = [](const std::string &target_address) -> std::string {
std::vector<std::pair<std::string, boost::asio::ip::udp>> test_addresses;
if (!target_address.empty()) {

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.

Is this target address must be ip, if so we can check whether it's ipv4 or ipv6 and use the protocol correspondingly. We don't need to try both everytime?

Comment thread python/ray/_common/network_utils.py Outdated
"""IP address by which the local node can be reached *from* the `address`.

If no address is given, defaults to public DNS servers for detection. For
performance, the result is cached when using the default address (empty string).

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 does the performance matter here? I'd imagine that for each Ray node, we only detect it once and use it for all the other places by passing node_ip_address around.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Got it. The reason I did this was that I had a create_socket function that’s called in many places, and it called node_ip_address_from_perspective() to automatically decide which socket to create.

Now I understand your point — I’ll try using node_ip_address directly to decide which socket to create, so node_ip_address_from_perspective() won’t need to be cached.

Comment thread python/ray/_private/node.py Outdated
@@ -880,7 +885,7 @@ def _get_unused_port(self, allocated_ports=None):
if allocated_ports is None:
allocated_ports = set()

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s = create_socket(socket.SOCK_STREAM)

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.

At this point, we should alway know the node-ip-address and whether it's ipv4 or ipv6 and we should select AF_INET based on that so I think the code should be like

s = socket.socket(socket.AF_INET6 if is_ip_ipv6(self.node_ip_address) else socket.AF_INET, socket.SOCK_STREAM)

We don't need another function for it.

@Yicheng-Lu-llll Yicheng-Lu-llll Sep 17, 2025 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I see your point!
My original idea was to add a safe helper create_socket for cases where node_ip_address wasn’t set yet or was hard to retrieve (but that assumption seems turn out to be false). It would:

    node_ip = get_node_ip_address() # which might call `node_ip_address_from_perspective`
    family = socket.AF_INET6 if is_ipv6_ip(node_ip) else socket.AF_INET

    return socket.socket(family, socket_type)

This lets call sites avoid worrying about IPv4 vs. IPv6.
Since create_socket is used in many places, I cached node_ip_address_from_perspective() to avoid repeated lookups.

I’ll try using node_ip_address directly in all places, which should let us remove the cache entirely for node_ip_address_from_perspective()

Comment thread python/ray/_private/services.py Outdated
# NOTE: This API should not be used when you obtain the
# IP address when ray.init is not called because
# it cannot find the IP address if it is specified by
# ray start --node-ip-address. You should instead use
# get_cached_node_ip_address.
def get_node_ip_address(address="8.8.8.8:53"):
def get_node_ip_address(address=""):

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.

Suggested change
def get_node_ip_address(address=""):
def get_node_ip_address(address=None):

Comment thread python/ray/_private/test_utils.py Outdated
@@ -776,7 +776,7 @@ def wait_until_server_available(address, timeout_ms=5000, retry_interval_ms=100)
time_elapsed = 0
start = time.time()
while time_elapsed <= timeout_ms:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s = create_socket(socket.SOCK_STREAM)

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.

we should pick family based on whether address is ipv4 or ipv6

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Got it — my previous thought was to unify the behavior based on the node IP. I’ll make the change!

Comment thread src/ray/util/network_util.cc Resolved
Comment thread src/ray/util/network_util.cc Resolved
Comment thread src/ray/util/network_util.cc Resolved
Comment thread src/ray/util/network_util.cc Resolved
Comment thread src/ray/util/network_util.cc Resolved
Comment thread src/ray/util/network_util.cc Resolved
Comment thread src/ray/util/network_util.cc Resolved
@Yicheng-Lu-llll

Copy link
Copy Markdown
Member Author

I’m going to reset the commit to b473c14, which is the last commit before I started reverting and debugging. I’ll update the branch once the flaky tests are fixed.

@Yicheng-Lu-llll
Yicheng-Lu-llll force-pushed the support-ipv6-for-socket branch from a779dec to b473c14 Compare October 13, 2025 21:23
host = "127.0.0.1"
port = self._get_unused_port(
socket.AF_INET6 if is_ipv6(host) else socket.AF_INET
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: IPv6 Support Issue in Localhost Handling

The code hardcodes host = "127.0.0.1" (IPv4) and then immediately checks is_ipv6(host) to determine socket family. Since "127.0.0.1" is always IPv4, is_ipv6(host) will always return False, effectively disabling IPv6 support in this code path. In IPv6-only environments, this could prevent proper functionality. The code should use get_localhost_ip() instead to get the appropriate localhost address for the system.

Fix in Cursor Fix in Web

ip = ray.get(workers[0].get_node_ip.remote())
port = ray.get(workers[0].find_free_port.remote())
address = f"tcp://{build_address(ip, port)}"
address = f"tcp://{ip}:{port}"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: IPv6 Address Formatting Error

The code was changed from address = f"tcp://{build_address(ip, port)}" to address = f"tcp://{ip}:{port}". This breaks IPv6 address formatting because IPv6 addresses must be wrapped in brackets when combined with a port (e.g., "[::1]:8080" not "::1:8080"). The build_address() function handles this correctly, but the manual string formatting does not, resulting in malformed URLs for IPv6 addresses.

Fix in Cursor Fix in Web

@jjyao

jjyao commented Oct 29, 2025

Copy link
Copy Markdown
Contributor

Failed windows and mac tests are unrelated.

@jjyao
jjyao merged commit a6bc1fe into ray-project:master Oct 29, 2025
edoakes pushed a commit that referenced this pull request Oct 31, 2025
This PR is a follow-up to
#56147 (comment)

The `find_free_port()` function was duplicated across multiple locations
in the codebase. This PR consolidates all implementations into a single
canonical location.

---------

Signed-off-by: Yicheng-Lu-llll <[email protected]>
YoussefEssDS pushed a commit to YoussefEssDS/ray that referenced this pull request Nov 8, 2025
Signed-off-by: Yicheng-Lu-llll <[email protected]>
Signed-off-by: Yicheng-Lu-llll <[email protected]>
Signed-off-by: Jiajun Yao <[email protected]>
Co-authored-by: Jiajun Yao <[email protected]>
YoussefEssDS pushed a commit to YoussefEssDS/ray that referenced this pull request Nov 8, 2025
This PR is a follow-up to
ray-project#56147 (comment)

The `find_free_port()` function was duplicated across multiple locations
in the codebase. This PR consolidates all implementations into a single
canonical location.

---------

Signed-off-by: Yicheng-Lu-llll <[email protected]>
landscapepainter pushed a commit to landscapepainter/ray that referenced this pull request Nov 17, 2025
Signed-off-by: Yicheng-Lu-llll <[email protected]>
Signed-off-by: Yicheng-Lu-llll <[email protected]>
Signed-off-by: Jiajun Yao <[email protected]>
Co-authored-by: Jiajun Yao <[email protected]>
landscapepainter pushed a commit to landscapepainter/ray that referenced this pull request Nov 17, 2025
This PR is a follow-up to
ray-project#56147 (comment)

The `find_free_port()` function was duplicated across multiple locations
in the codebase. This PR consolidates all implementations into a single
canonical location.

---------

Signed-off-by: Yicheng-Lu-llll <[email protected]>
SheldonTsen pushed a commit to SheldonTsen/ray that referenced this pull request Dec 1, 2025
This PR is a follow-up to
ray-project#56147 (comment)

The `find_free_port()` function was duplicated across multiple locations
in the codebase. This PR consolidates all implementations into a single
canonical location.

---------

Signed-off-by: Yicheng-Lu-llll <[email protected]>
Future-Outlier pushed a commit to Future-Outlier/ray that referenced this pull request Dec 7, 2025
Signed-off-by: Yicheng-Lu-llll <[email protected]>
Signed-off-by: Yicheng-Lu-llll <[email protected]>
Signed-off-by: Jiajun Yao <[email protected]>
Co-authored-by: Jiajun Yao <[email protected]>
Signed-off-by: Future-Outlier <[email protected]>
Future-Outlier pushed a commit to Future-Outlier/ray that referenced this pull request Dec 7, 2025
This PR is a follow-up to
ray-project#56147 (comment)

The `find_free_port()` function was duplicated across multiple locations
in the codebase. This PR consolidates all implementations into a single
canonical location.

---------

Signed-off-by: Yicheng-Lu-llll <[email protected]>
Signed-off-by: Future-Outlier <[email protected]>
dstrodtman added a commit that referenced this pull request Oct 13, 2026
## Why are these changes needed?

Some files under `doc/source/core/` aren't shown on any docs page, and
nothing else in the repo references them. Docs example code should
either appear on a page, be a test wrapper for content that does, or be
deleted. This PR applies that rule to Ray Core:

| File | Status before | Change |
| --- | --- | --- |
| `doc_code/original_resource_unavailable_example.py` | Run by the
`source/core/doc_code/*.py` catch-all in `doc/BUILD.bazel`. #24544 added
it as a `literalinclude` on the placement group page, and a later
rewrite of that page dropped the include. It demonstrates scheduling a
task into a placement group's reserved CPUs, not a regression check, so
it isn't worth moving into `python/ray/tests/`. | Deleted |
| `examples/lm/` (`ray_train.py`, `ray_train.sh`, `preprocess.sh`,
`lm-cluster.yaml`) | Not tested. Referenced only by each other. Pins
`fairseq==0.8.0` and is what's left of a removed fairseq language-model
example. Its only recent edits are repo-wide sweeps such as the IPv6
work in #55281, #56147, and #58304. | Deleted |
| `examples/rdt/grpo_contextual_bandits.py` | Not tested. No page
includes it. #57961 added it as the companion script for the [Ray Direct
Transport blog
post](https://anyscale.com/blog/ray-direct-transport-rdma-support-in-ray-core),
which links to it by commit permalink
(`blob/c4f0c24.../doc/source/ray-core/examples/rdt/...`), so the blog
link keeps working after this deletion. | Deleted |

The catch-all's `exclude` list doesn't change. Deleting
`original_resource_unavailable_example.py` only removes the one test
target the glob generated for it.

### Questions for Ray Core maintainers

Please confirm or push back per file:

- `original_resource_unavailable_example.py`: if you'd rather keep this
behavior under test, I can move it to `python/ray/tests/` instead of
deleting it.
- `examples/rdt/grpo_contextual_bandits.py`: was a docs page planned for
it? If so, I can keep the script and add a page with a `literalinclude`
and a test instead. If the RDT team wants it kept as a standalone
example, say where it should live.

## Related issue number

None. I searched open PRs for each filename and found no overlap.

## Checks

- [x] I've signed off every commit (DCO).
- [x] `pre-commit run --files <changed files>`: passed, including the
master-link check.
- [x] `git grep` for `examples/lm`, `examples/rdt`,
`original_resource_unavailable`, and `grpo_contextual`: no remaining
references.
- [ ] I didn't run a local Sphinx build. No page referenced these files.

AI assistance (Claude Code) was used to prepare this PR. I reviewed
every changed line.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Douglas Strodtman <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants