Skip to content

Conversation

@Yicheng-Lu-llll
Copy link
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
"""
with closing(socket.socket(family, socket.SOCK_STREAM)) as s:
s.bind(("", 0))
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Copy link
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
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
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
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
Copy link
Collaborator

edoakes commented Oct 31, 2025

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
Member Author

@edoakes Thanks! CI has passed

@edoakes edoakes merged commit 227470f into ray-project:master Oct 31, 2025
7 checks passed
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]>
Aydin-ab pushed a commit to Aydin-ab/ray-aydin that referenced this pull request Nov 19, 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: Aydin Abiar <[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]>
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