Skip to content

[Core] Consolidate find_free_port to network_utils - #58304

Merged
edoakes merged 4 commits into
ray-project:masterfrom
Yicheng-Lu-llll:find-free-port-typehint-and-relocation
Oct 31, 2025
Merged

edoakes merged 4 commits into
ray-project:masterfrom
Yicheng-Lu-llll:find-free-port-typehint-and-relocation

Conversation

@Yicheng-Lu-llll

Copy link
Copy Markdown
Member

Description

Briefly describe what this PR accomplishes and why it's needed.

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.

Related issues

Additional information

@Yicheng-Lu-llll
Yicheng-Lu-llll force-pushed the find-free-port-typehint-and-relocation branch from e40b6fc to ec9c797 Compare October 30, 2025 07:44
@Yicheng-Lu-llll
Yicheng-Lu-llll force-pushed the find-free-port-typehint-and-relocation branch from ec9c797 to e1d0e3e Compare October 30, 2025 07:47
@Yicheng-Lu-llll
Yicheng-Lu-llll marked this pull request as ready for review October 30, 2025 18:28
@Yicheng-Lu-llll
Yicheng-Lu-llll requested review from a team, edoakes and jjyao as code owners October 30, 2025 18:28
@ray-gardener ray-gardener Bot added core Issues that should be addressed in Ray Core community-contribution Contributed by the community labels Oct 30, 2025
Comment thread python/ray/_common/network_utils.py Outdated
"""
with closing(socket.socket(family, socket.SOCK_STREAM)) as s:
s.bind(("", 0))
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

what's the purpose of SO_REUSEADDR here? is it to allow subsequent usage of the port to bind to it even if it hasn't been freed by the kernel yet?

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.

Good catch—this was carried over from the original implementation in python/ray/air/_internal/util.py, and I didn’t re‑evaluate it at the time. I think the intention, as you said, was to allow subsequent processes to bind to the port even if the kernel hadn’t freed it yet, since the purpose of this function is to get a free port.

In reality, a socket that is only bound (not listening or connected) doesn’t enter TIME_WAIT, so the port is freed on close and the next process can bind immediately. Also, SO_REUSEADDR only affects the socket that sets it during its own bind(); it doesn’t prepare the port for others.

I’ll remove this line. Thank you!

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 noticed we also have the following in codebase:

s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("", 0))

Because SO_REUSEADDR is set, it can return a port that is only bindable with SO_REUSEADDR (e.g., while there are lingering connections in TIME_WAIT). A subsequent bind() (without SO_REUSEADDR) may then fail. So it’s safer to also remove the line here.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the detailed explanation! I agree with the conclusions.

@Yicheng-Lu-llll
Yicheng-Lu-llll requested a review from a team as a code owner October 31, 2025 01:56
Signed-off-by: Yicheng-Lu-llll <[email protected]>
@Yicheng-Lu-llll
Yicheng-Lu-llll requested a review from a team as a code owner October 31, 2025 02:13
@edoakes edoakes added the go add ONLY when ready to merge, run all tests label Oct 31, 2025
@edoakes

edoakes commented Oct 31, 2025

Copy link
Copy Markdown
Collaborator

I've added the go label to trigger premerge CI: https://buildkite.com/ray-project/premerge/builds/52987

Please ping me when this passes

@Yicheng-Lu-llll

Copy link
Copy Markdown
Member Author

@edoakes Thanks! CI has passed

@edoakes
edoakes merged commit 227470f into ray-project:master Oct 31, 2025
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
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
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.

3 participants