Skip to content

[Utils] Move network/socket utilities from common.py to network.py - #20646

Merged
hnyls2002 merged 5 commits into
mainfrom
move-network-utils
Mar 16, 2026
Merged

hnyls2002 merged 5 commits into
mainfrom
move-network-utils

Conversation

@hnyls2002

@hnyls2002 hnyls2002 commented Mar 16, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Move 12+ network/socket functions from common.py to network.py: try_bind_socket, is_port_available, get_free_port, bind_port, get_open_port, wait_port_available, find_process_using_port, get_local_ip_auto, get_local_ip_by_nic, get_local_ip_by_remote, is_valid_ipv6_address, _get_addrinfos_for_bind
  • Move ZMQ socket utilities: get_zmq_socket, get_zmq_socket_on_host, config_socket
  • Update all import sites (18 files) to import directly from sglang.srt.utils.network
  • No re-exports in common.py — clean separation between network and non-network utilities

Follows up on #20643.

@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 refactors the utility codebase by consolidating all network-related functions into a new, dedicated network.py file. This change improves code organization, making network utilities easier to locate, maintain, and test independently. By separating concerns, it enhances modularity and reduces the cognitive load when working with the common.py file, which previously contained a broad mix of unrelated utilities.

Highlights

  • Code Refactoring: All network-related utility functions and their corresponding imports have been moved from python/sglang/srt/utils/common.py to a dedicated file, python/sglang/srt/utils/network.py.
  • Network Utilities Consolidation: Functions for port management (get_open_port, is_port_available, try_bind_socket, get_free_port, bind_port, wait_port_available, find_process_using_port), IP address detection (is_valid_ipv6_address, get_local_ip_by_nic, get_local_ip_by_remote, get_local_ip_auto), and ZeroMQ socket handling (get_zmq_socket, get_zmq_socket_on_host, config_socket) are now centralized in network.py.
  • Dependency Management: Imports for ipaddress, socket, zmq, logging, os, time, psutil, and specific typing utilities have been adjusted to reflect their new location and usage.

🧠 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
    • Removed imports for ipaddress, socket, and zmq.
    • Removed network utility functions including find_process_using_port, wait_port_available, _get_addrinfos_for_bind, try_bind_socket, is_port_available, get_free_port, get_zmq_socket, get_zmq_socket_on_host, config_socket, bind_port, get_open_port, is_valid_ipv6_address, get_local_ip_by_nic, get_local_ip_by_remote, and get_local_ip_auto.
  • python/sglang/srt/utils/network.py
    • Added imports for logging, os, time, psutil, zmq, and additional typing utilities.
    • Added network utility functions including get_open_port, is_valid_ipv6_address, find_process_using_port, wait_port_available, _get_addrinfos_for_bind, try_bind_socket, is_port_available, get_free_port, bind_port, get_zmq_socket_on_host, config_socket, get_local_ip_by_nic, get_local_ip_by_remote, get_local_ip_auto, and get_zmq_socket.
Activity
  • The pull request was opened with a 'WIP' (Work In Progress) status, indicating that the author, hnyls2002, is still actively developing these changes and may not yet be ready for a full review.
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 refactors the codebase by moving network-related utility functions from common.py to a dedicated network.py file. This is a good step towards better code organization. I've identified a critical bug in the wait_port_available function that could lead to an AttributeError or UnboundLocalError, and I've provided a suggestion to fix it.

Comment thread python/sglang/srt/utils/network.py
@hnyls2002 hnyls2002 changed the title [Utils] Move all network utils into network.py [Utils] Move network/socket utilities from common.py to network.py Mar 16, 2026
@hnyls2002
hnyls2002 merged commit f0458e0 into main Mar 16, 2026
71 of 90 checks passed
@hnyls2002
hnyls2002 deleted the move-network-utils branch March 16, 2026 03:35
KHAEntertainment pushed a commit to Clarit-AI/Engram that referenced this pull request Mar 31, 2026
….py to network.py (sgl-project#20646)

Resolved conflicts by accepting upstream's network import restructuring in all files.
New file: python/sglang/srt/utils/network.py
Restored: encode_grpc_server.py, elastic_ep files (deleted in HEAD, updated in upstream).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
JustinTong0323 pushed a commit to JustinTong0323/sglang that referenced this pull request Apr 7, 2026
@kirangowdac

kirangowdac commented Apr 14, 2026

Copy link
Copy Markdown

@hnyls2002,

This change is breaking many UTs as get_Zmq_socket moved to network but did not added in 'python/sglang/srt/utils/init.py'. Causing get_zmq_socket not found.

Can you pls handle 'init.py' as well accordingly.

licson pushed a commit to licson/sglang that referenced this pull request Apr 28, 2026
is_port_available was moved from common.py to network.py in sgl-project#20646 but the
package-level re-export was not preserved. This breaks sglang-router 0.3.2
launch_server and any other downstream consumer that imports
`from sglang.srt.utils import is_port_available`.

Fixes sgl-project#23535
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants