← Back to Pull requests

shared/network: implement Happy Eyeballs via Go's built-in dialer #18234

Merged
mbeijen merged 1 files (43 lines) on May 05, 2026 10:18 UTC
View on GitHub
mbeijen mbeijen commented on May 04, 2026 15:29 UTC
Author

Replace the manual DNS-resolve + sequential-address loop in RFC3493Dialer with a direct call to net.Dialer.DialContext using the original hostname. Go's dialer has implemented RFC 8305 Happy Eyeballs since Go 1.12: it races IPv6 and IPv4 concurrently with a ~250 ms fallback, eliminating the 10 s per-address stall that occurred when IPv6 was present but unreachable.

TCP keepalive config and TCP_USER_TIMEOUT are preserved. Context propagation to DNS resolution is also fixed as a side-effect.

I ran into this because I was trying to get lxd working on a host with a not-working IPv6. Instead of falling back to ipv4, it ran into timeouts.
The Happy Eyeballs path is a better user experience and also much less code.

Checklist

  • I have read the contributing guidelines and attest that all commits in this PR are signed off, cryptographically signed, and follow this project's commit structure.

  • I have checked and added or updated relevant documentation.
  • Diff: 43 lines (+16 / -27) across 1 files Reviewer: simondeziel Review duration: 1128m
    All Vouch review confidence checks passed
    Residual risk 0.03 = Change Risk (0.26) × (1 − Review Confidence (0.89)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.26 · 43 lines diff across 1 files
    0.26
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.87 · 5 substantive comment(s)
    0.87
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    89%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.226
    Real PR Conversation Timeline
    12 items
    simondeziel simondeziel commented on code on May 04, 2026 15:55 UTC
    Code Review
    shared/network.go:1
    @@ -20,45 +20,29 @@ const connectErrorPrefix = "Cannot connect to"
     
     // RFC3493Dialer connects to the specified server and returns the connection.
     // If the connection cannot be established then an error with the connectErrorPrefix is returned.
    +// Go's built-in Happy Eyeballs (RFC 8305) races IPv6 and IPv4 concurrently when a hostname is passed.
     func RFC3493Dialer(ctx context.Context, network string, address string) (net.Conn, error) {
    -	host, port, err := net.SplitHostPort(address)
    -	if err != nil {
    -		return nil, err
    +	kaConfig, userTimeout := tcp.KeepAliveTimeouts()
    +
    +	dialer := net.Dialer{
    +		Timeout:         30 * time.Second,

    Why go from 10s to 30s?

    Copilot Copilot commented on code on May 04, 2026 15:57 UTC
    Code Review
    shared/network.go:1
    @@ -20,45 +20,29 @@ const connectErrorPrefix = "Cannot connect to"
     
     // RFC3493Dialer connects to the specified server and returns the connection.
     // If the connection cannot be established then an error with the connectErrorPrefix is returned.
    +// Go's built-in Happy Eyeballs (RFC 8305) races IPv6 and IPv4 concurrently when a hostname is passed.
     func RFC3493Dialer(ctx context.Context, network string, address string) (net.Conn, error) {
    -	host, port, err := net.SplitHostPort(address)
    -	if err != nil {
    -		return nil, err
    +	kaConfig, userTimeout := tcp.KeepAliveTimeouts()
    +
    +	dialer := net.Dialer{
    +		Timeout:         30 * time.Second,

    The dial timeout has been increased from 10s (previous per-address timeout) to 30s. This changes failure latency for cases where only one address is attempted (e.g. single A record, direct IP, or tcp4/tcp6), potentially making the client hang noticeably longer on unreachable targets. Consider keeping the previous 10s default here, or deriving the timeout from ctx.Deadline (and otherwise leaving Timeout unset) so callers control the connect deadline.

    Copilot Copilot commented on code on May 04, 2026 15:57 UTC
    Code Review
    shared/network.go:1
    @@ -20,45 +20,29 @@ const connectErrorPrefix = "Cannot connect to"
     
     // RFC3493Dialer connects to the specified server and returns the connection.
     // If the connection cannot be established then an error with the connectErrorPrefix is returned.
    +// Go's built-in Happy Eyeballs (RFC 8305) races IPv6 and IPv4 concurrently when a hostname is passed.
     func RFC3493Dialer(ctx context.Context, network string, address string) (net.Conn, error) {
    -	host, port, err := net.SplitHostPort(address)
    -	if err != nil {
    -		return nil, err
    +	kaConfig, userTimeout := tcp.KeepAliveTimeouts()
    +
    +	dialer := net.Dialer{
    +		Timeout:         30 * time.Second,
    +		KeepAliveConfig: kaConfig,
     	}
     
    -	addrs, err := net.LookupHost(host)
    +	c, err := dialer.DialContext(ctx, network, address)
     	if err != nil {
    -		return nil, err
    +		return nil, fmt.Errorf("%s: %s (%w)", connectErrorPrefix, address, err)
     	}

    RFC3493Dialer now wraps any DialContext error with the "Cannot connect to" prefix. This includes non-connectivity errors like malformed addresses (e.g. "missing port in address"), which will then be misclassified by IsConnectionError (it only checks for that prefix) and may incorrectly trigger retry/continue logic in callers. Consider validating/parsing the address first (at least for tcp/tcp4/tcp6) and returning the raw error for invalid input, or otherwise ensuring only genuine connection failures get the prefix.

    copilot-pull-request-reviewer[bot] copilot-pull-request-reviewer[bot] commented on May 04, 2026 15:57 UTC
    Reviewer

    Pull request overview

    This PR updates LXD’s shared network dial helper (RFC3493Dialer) to rely on Go’s net.Dialer.DialContext with the original hostname so Go’s built-in Happy Eyeballs behavior can select between IPv6/IPv4 more effectively (rather than manually resolving and trying addresses sequentially).

    Changes:

  • Replaces manual LookupHost + sequential per-IP dialing with a single DialContext call using the original address string.

  • Preserves TCP keepalive configuration and TCP_USER_TIMEOUT setup on successful TCP connections.

  • Standardizes connection failures to include the connectErrorPrefix wrapper.
  • mbeijen mbeijen commented on code on May 04, 2026 16:09 UTC
    Code Review
    shared/network.go:1
    @@ -20,45 +20,29 @@ const connectErrorPrefix = "Cannot connect to"
     
     // RFC3493Dialer connects to the specified server and returns the connection.
     // If the connection cannot be established then an error with the connectErrorPrefix is returned.
    +// Go's built-in Happy Eyeballs (RFC 8305) races IPv6 and IPv4 concurrently when a hostname is passed.
     func RFC3493Dialer(ctx context.Context, network string, address string) (net.Conn, error) {
    -	host, port, err := net.SplitHostPort(address)
    -	if err != nil {
    -		return nil, err
    +	kaConfig, userTimeout := tcp.KeepAliveTimeouts()
    +
    +	dialer := net.Dialer{
    +		Timeout:         30 * time.Second,

    yeah I set this back to 10s now, thanks for catching

    mbeijen mbeijen commented on code on May 04, 2026 16:09 UTC
    Code Review
    shared/network.go:1
    @@ -20,45 +20,29 @@ const connectErrorPrefix = "Cannot connect to"
     
     // RFC3493Dialer connects to the specified server and returns the connection.
     // If the connection cannot be established then an error with the connectErrorPrefix is returned.
    +// Go's built-in Happy Eyeballs (RFC 8305) races IPv6 and IPv4 concurrently when a hostname is passed.
     func RFC3493Dialer(ctx context.Context, network string, address string) (net.Conn, error) {
    -	host, port, err := net.SplitHostPort(address)
    -	if err != nil {
    -		return nil, err
    +	kaConfig, userTimeout := tcp.KeepAliveTimeouts()
    +
    +	dialer := net.Dialer{
    +		Timeout:         30 * time.Second,
    +		KeepAliveConfig: kaConfig,
     	}
     
    -	addrs, err := net.LookupHost(host)
    +	c, err := dialer.DialContext(ctx, network, address)
     	if err != nil {
    -		return nil, err
    +		return nil, fmt.Errorf("%s: %s (%w)", connectErrorPrefix, address, err)
     	}

    I improved the error message situation, similar to how it was before

    mbeijen mbeijen commented on code on May 04, 2026 16:09 UTC
    Code Review
    shared/network.go:1
    @@ -20,45 +20,29 @@ const connectErrorPrefix = "Cannot connect to"
     
     // RFC3493Dialer connects to the specified server and returns the connection.
     // If the connection cannot be established then an error with the connectErrorPrefix is returned.
    +// Go's built-in Happy Eyeballs (RFC 8305) races IPv6 and IPv4 concurrently when a hostname is passed.
     func RFC3493Dialer(ctx context.Context, network string, address string) (net.Conn, error) {
    -	host, port, err := net.SplitHostPort(address)
    -	if err != nil {
    -		return nil, err
    +	kaConfig, userTimeout := tcp.KeepAliveTimeouts()
    +
    +	dialer := net.Dialer{
    +		Timeout:         30 * time.Second,

    my bad, thanks for noticing!

    simondeziel simondeziel approved these changes on May 04, 2026 17:30 UTC
    Approved Reviewer

    Approved these changes.

    mbeijen mbeijen commented on May 04, 2026 19:13 UTC
    Author

    Thanks for approving @simondeziel

    As far as I can see the CI failures are pre-existing, and have nothing to do with this PR; the documentation one hits a timeout and the other errors with 'failed updating instance types' which is because it connects to images.lxd.canonical.com which maps to 127.0.0.1 in CI and gets 'connection refused', and these happen also on other PRs

    simondeziel simondeziel commented on May 04, 2026 19:16 UTC
    Contributor

    Thanks for approving @simondeziel

    Thanks for the PR!

    As far as I can see the CI failures are pre-existing, and have nothing to do with this PR; the documentation one hits a timeout and the other errors with 'failed updating instance types' which is because it connects to images.lxd.canonical.com which maps to 127.0.0.1 in CI and gets 'connection refused', and these happen also on other PRs

    Yep, I confirm, not related to the changes at hand :)

    tomponline tomponline approved these changes on May 05, 2026 10:17 UTC
    Approved Reviewer

    Thanks for this.

    One thing I was immediately concerned about is whether the loop of the addresses returned from net.LookupHost was originally intended to support hosts with multiple A records (where LXD would attempt to connect to each one, rather than only trying the first one returned by the DNS resolver), and not just a host with both A and AAAA records.

    However I found https://github.com/canonical/lxd/commit/9f7a754783bc2ef4391ee2435db37588cdc8444e that originally introduced this and it does seem that it was intended for trying IPv6 connectivity too.

    It could still be the case however that introducing this change may break some users who can come (perhaps inadvertently in the case of misconfigured DNS) to depend on this behaviour.

    But I think its worth merging anyway as this gives more predictable, modern and standard behaviour.

    Reviewers
    Assignees
    mbeijen mbeijen
    Labels
    risk: low confidence: 89% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 25.6% (LOW) · Higher = larger blast radius
    25.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 89% · Lower = rushed or shallow review
    89%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 2.8% · Safe to merge
    2.8%
    Repository