Skip to content

Fix socket utilities and reserve_port for IPv6 dual-stack support#20491

Merged
hnyls2002 merged 5 commits intosgl-project:mainfrom
psaab:ipv6-reserve-port
Mar 15, 2026
Merged

Fix socket utilities and reserve_port for IPv6 dual-stack support#20491
hnyls2002 merged 5 commits intosgl-project:mainfrom
psaab:ipv6-reserve-port

Conversation

@psaab
Copy link
Copy Markdown
Contributor

@psaab psaab commented Mar 13, 2026

Summary

  • Add get_addrinfos_for_bind(host, port) helper that uses getaddrinfo(AF_UNSPEC, AI_ADDRCONFIG | AI_PASSIVE) to query which address families are configured on the host, returning proper
    sockaddrs for bind()
  • Update is_port_available, get_free_port, bind_port, get_open_port, and get_local_ip_by_remote in common.py to use get_addrinfos_for_bind instead of hardcoding AF_INET
  • Update reserve_port and normalize_base_url in utils.py to use get_addrinfos_for_bind and NetworkAddress respectively for IPv6 compatibility

Motivation

The existing socket utilities hardcode AF_INET (IPv4), which fails on IPv6-only networks. By using getaddrinfo with AI_ADDRCONFIG, the OS tells us which address families are actually
configured, so we try the right one first without hardcoding fallback order.

Test plan

  • Verified on dual-stack host: get_addrinfos_for_bind() returns both AF_INET6 and AF_INET entries
  • is_port_available, get_free_port, bind_port, get_open_port all work unchanged on IPv4 hosts
  • reserve_port correctly binds on both IPv4 and IPv6 addresses
  • normalize_base_url correctly wraps IPv6 addresses in brackets (e.g. http://[::1]:8000)

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@hnyls2002
Copy link
Copy Markdown
Collaborator

Please mention me when this PR is ready for review. @psaab

@psaab
Copy link
Copy Markdown
Contributor Author

psaab commented Mar 13, 2026

@hnyls2002 I am going to just do this bit by bit as some of the vibe coded stuff isn't relavent or would you rather have more changes in one big PR? Otherwise I think this one is ready

@hnyls2002
Copy link
Copy Markdown
Collaborator

Sure, I will review later if this one is OK

@psaab
Copy link
Copy Markdown
Contributor Author

psaab commented Mar 14, 2026

@hnyls2002 Any idea when this could get reviewed so I can go clean up the rest of the changes?

Comment thread python/sglang/srt/utils/common.py Outdated
def is_port_available(port):
"""Return whether a port is available."""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
for family, socktype, proto, _, sockaddr in get_addrinfos_for_bind(port):
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.

Have you tested the PR?

Your definition:

def get_addrinfos_for_bind(host=None, port=0):

Your are using with:

for family, socktype, proto, _, sockaddr in get_addrinfos_for_bind(port):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

facepalm, i cleaned up the diff and didn't catch this, sorry.. added test cases.

@psaab psaab force-pushed the ipv6-reserve-port branch 4 times, most recently from 9860201 to 479762c Compare March 15, 2026 11:56
Add get_addrinfos_for_bind(host, port) helper that uses
getaddrinfo(AF_UNSPEC, AI_ADDRCONFIG | AI_PASSIVE) to query which
address families are configured on the host, returning proper
sockaddrs for bind().

Update is_port_available, get_free_port, bind_port, get_open_port,
and get_local_ip_by_remote to use get_addrinfos_for_bind instead of
hardcoding AF_INET.

Update reserve_port and normalize_base_url to use
get_addrinfos_for_bind and NetworkAddress respectively for IPv6
compatibility.
@psaab psaab force-pushed the ipv6-reserve-port branch from 479762c to 8afba4f Compare March 15, 2026 12:01
@hnyls2002 hnyls2002 merged commit 1145805 into sgl-project:main Mar 15, 2026
115 of 138 checks passed
Wangzheee pushed a commit to Wangzheee/sglang that referenced this pull request Mar 21, 2026
0-693 pushed a commit to 0-693/sglang that referenced this pull request Mar 25, 2026
JustinTong0323 pushed a commit to JustinTong0323/sglang that referenced this pull request Apr 7, 2026
yhyang201 pushed a commit to yhyang201/sglang that referenced this pull request Apr 22, 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