Skip to content

Fix dual-stack socket handling: IPV6_V6ONLY, IPv4-first, is_port_available all-family check - #20643

Merged
hnyls2002 merged 7 commits into
mainfrom
improve-ipv6
Mar 16, 2026
Merged

hnyls2002 merged 7 commits into
mainfrom
improve-ipv6

Conversation

@hnyls2002

@hnyls2002 hnyls2002 commented Mar 15, 2026

Copy link
Copy Markdown
Collaborator

Changes

  • Set IPV6_V6ONLY=1 on IPv6 sockets in try_bind_socket — isolate IPv4/IPv6 to eliminate platform-dependent dual-stack behavior
  • Fix is_port_available to check all address families with SO_REUSEADDR — previously only checked first-success family, missing conflicts on the other family under IPV6_V6ONLY
  • Sort _get_addrinfos_for_bind IPv4-first for consistent default across platforms
  • Refactor get_open_port to use is_port_available (all-family) for SGLANG_PORT path; use default reuse_addr=True for ephemeral path to match server bind behavior

Nits

  • Rewrite _get_addrinfos_for_bind dedup from set-comprehension side-effect hack to explicit loop
  • Move OverflowError catch from is_port_available down to try_bind_socket
  • Add tests for reserve_port, release_port, normalize_base_url, port-occupied detection

Test plan

  • Unit tests pass on both macOS and Linux
  • /rerun-stage stage-a-cpu-only

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 significantly enhances the network handling capabilities, particularly for IPv6, by refining socket binding logic and ensuring more reliable port allocation. The changes aim to improve the stability and predictability of network operations within the system, complemented by new tests to validate the behavior of key utility functions.

Highlights

  • Improved IPv6 Handling: Configured IPv6 sockets to explicitly use IPV6_V6ONLY to prevent dual-stack behavior, ensuring more predictable IPv6 network interactions.
  • Enhanced Port Binding Logic: Refactored the address information deduplication in _get_addrinfos_for_bind for clarity and robustness, and expanded exception handling in try_bind_socket to include OverflowError.
  • Reliable Open Port Retrieval: Modified get_open_port to explicitly disable SO_REUSEADDR when searching for an available port, ensuring that the returned port is truly unique and not in use by a lingering socket.
  • New Utility Function Tests: Added comprehensive unit tests for the reserve_port and normalize_base_url utility functions, covering various scenarios including IPv4, IPv6, hostnames, and error conditions.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • python/sglang/srt/utils/common.py
    • Refactored address information deduplication in _get_addrinfos_for_bind for improved clarity and robustness.
    • Added IPV6_V6ONLY socket option for IPv6 sockets to prevent dual-stack behavior.
    • Expanded exception handling in try_bind_socket to catch OverflowError during socket binding.
    • Adjusted exception handling in is_port_available to specifically catch OSError.
    • Ensured get_open_port binds to a unique port by setting reuse_addr=False in try_bind_socket.
  • test/registered/utils/test_socket_utils.py
    • Imported normalize_base_url, release_port, and reserve_port for testing.
    • Added new test cases for TestReservePort covering functionality, custom ranges, and error handling.
    • Included new test cases for TestNormalizeBaseUrl to validate IPv4, IPv6, hostname, and deprecated scheme handling.
Activity
  • No human activity has been recorded on this pull request yet.
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.

@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

This pull request introduces several improvements to IPv6 network handling and related socket utilities. The changes include refactoring address info deduplication for better readability, correctly setting the IPV6_V6ONLY socket option for IPv6 sockets, refining exception handling during port binding, and making get_open_port more reliable by disabling address reuse. Additionally, new tests are added for port reservation and URL normalization, which enhance test coverage. The changes are logical and well-implemented. I've found a minor issue in one of the new tests regarding an incorrect mock patch target and have provided a suggestion to fix it.

Comment thread test/registered/utils/test_socket_utils.py Outdated
@hnyls2002 hnyls2002 changed the title [ipv6] improve ipv6 network handling Improve IPv6 socket utilities and add tests Mar 15, 2026
@hnyls2002

Copy link
Copy Markdown
Collaborator Author

/tag-and-rerun-ci

@hnyls2002

Copy link
Copy Markdown
Collaborator Author

/rerun-ut test_socket_utils.py

@github-actions

Copy link
Copy Markdown
Contributor

❌ No register_cuda_ci() found in test/registered/utils/test_socket_utils.py.

This file may not be a registered CUDA CI test.

@hnyls2002 hnyls2002 changed the title Improve IPv6 socket utilities and add tests Fix dual-stack socket handling: IPV6_V6ONLY, IPv4-first ordering, is_port_available correctness Mar 15, 2026
@hnyls2002 hnyls2002 changed the title Fix dual-stack socket handling: IPV6_V6ONLY, IPv4-first ordering, is_port_available correctness Fix dual-stack socket handling: IPV6_V6ONLY, IPv4-first, is_port_available all-family check Mar 15, 2026
@hnyls2002

hnyls2002 commented Mar 15, 2026

Copy link
Copy Markdown
Collaborator Author

/rerun-ut test/registered/disaggregation/test_disaggregation_basic.py

@github-actions

Copy link
Copy Markdown
Contributor

❌ Ambiguous filename test_disaggregation_basic.py — matched 2 files:

  • test/registered/amd/disaggregation/test_disaggregation_basic.py
  • test/registered/disaggregation/test_disaggregation_basic.py

Please provide the full path, e.g. /rerun-ut test/registered/amd/disaggregation/test_disaggregation_basic.py

@github-actions

Copy link
Copy Markdown
Contributor

✅ Triggered /rerun-ut on 2-gpu-runner runner:

cd test/ && python3 registered/disaggregation/test_disaggregation_basic.py

@github-actions

Copy link
Copy Markdown
Contributor

🔗 View workflow run

@hnyls2002

hnyls2002 commented Mar 16, 2026

Copy link
Copy Markdown
Collaborator Author

/rerun-ut test_disaggregation_dp_attention.py

@github-actions

Copy link
Copy Markdown
Contributor

✅ Triggered /rerun-ut on 8-gpu-h20 runner:

cd test/ && python3 registered/distributed/test_disaggregation_dp_attention.py

@sgl-project sgl-project deleted a comment from github-actions Bot Mar 16, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🔗 View workflow run

@hnyls2002
hnyls2002 merged commit d852f26 into main Mar 16, 2026
62 of 79 checks passed
@hnyls2002
hnyls2002 deleted the improve-ipv6 branch March 16, 2026 00:17
Wangzheee pushed a commit to Wangzheee/sglang that referenced this pull request Mar 21, 2026
JustinTong0323 pushed a commit to JustinTong0323/sglang that referenced this pull request Apr 7, 2026
Chronostasys pushed a commit to MindLab-Research/sglang that referenced this pull request Aug 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant