Fix socket utilities and reserve_port for IPv6 dual-stack support#20491
Merged
hnyls2002 merged 5 commits intosgl-project:mainfrom Mar 15, 2026
Merged
Fix socket utilities and reserve_port for IPv6 dual-stack support#20491hnyls2002 merged 5 commits intosgl-project:mainfrom
hnyls2002 merged 5 commits intosgl-project:mainfrom
Conversation
Contributor
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
Collaborator
|
Please mention me when this PR is ready for review. @psaab |
Contributor
Author
|
@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 |
Collaborator
|
Sure, I will review later if this one is OK |
Contributor
Author
|
@hnyls2002 Any idea when this could get reviewed so I can go clean up the rest of the changes? |
hnyls2002
requested changes
Mar 15, 2026
| 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): |
Collaborator
There was a problem hiding this comment.
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):
Contributor
Author
There was a problem hiding this comment.
facepalm, i cleaned up the diff and didn't catch this, sorry.. added test cases.
9860201 to
479762c
Compare
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.
479762c to
8afba4f
Compare
hnyls2002
approved these changes
Mar 15, 2026
Merged
2 tasks
Wangzheee
pushed a commit
to Wangzheee/sglang
that referenced
this pull request
Mar 21, 2026
…l-project#20491) Co-authored-by: hnyls2002 <lsyincs@gmail.com>
0-693
pushed a commit
to 0-693/sglang
that referenced
this pull request
Mar 25, 2026
…l-project#20491) Co-authored-by: hnyls2002 <lsyincs@gmail.com>
JustinTong0323
pushed a commit
to JustinTong0323/sglang
that referenced
this pull request
Apr 7, 2026
…l-project#20491) Co-authored-by: hnyls2002 <lsyincs@gmail.com>
yhyang201
pushed a commit
to yhyang201/sglang
that referenced
this pull request
Apr 22, 2026
…l-project#20491) Co-authored-by: hnyls2002 <lsyincs@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
get_addrinfos_for_bind(host, port)helper that usesgetaddrinfo(AF_UNSPEC, AI_ADDRCONFIG | AI_PASSIVE)to query which address families are configured on the host, returning propersockaddrs for
bind()is_port_available,get_free_port,bind_port,get_open_port, andget_local_ip_by_remoteincommon.pyto useget_addrinfos_for_bindinstead of hardcodingAF_INETreserve_portandnormalize_base_urlinutils.pyto useget_addrinfos_for_bindandNetworkAddressrespectively for IPv6 compatibilityMotivation
The existing socket utilities hardcode
AF_INET(IPv4), which fails on IPv6-only networks. By usinggetaddrinfowithAI_ADDRCONFIG, the OS tells us which address families are actuallyconfigured, so we try the right one first without hardcoding fallback order.
Test plan
get_addrinfos_for_bind()returns bothAF_INET6andAF_INETentriesis_port_available,get_free_port,bind_port,get_open_portall work unchanged on IPv4 hostsreserve_portcorrectly binds on both IPv4 and IPv6 addressesnormalize_base_urlcorrectly wraps IPv6 addresses in brackets (e.g.http://[::1]:8000)