lima-vm/lima · error

expected *net.UDPAddr, got %v

Error message

expected *net.UDPAddr, got %v

What it means

freeport.UDP() binds a UDP socket on 127.0.0.1:0 and asserts l.LocalAddr() is a *net.UDPAddr to extract the assigned port. If the returned net.Addr is of another concrete type, this error is thrown. Like its TCP counterpart it should be unreachable on standard platforms.

Source

Thrown at pkg/freeport/freeport.go:47

		return 0, fmt.Errorf("unexpected port %d", port)
	}
	return port, nil
}

func UDP() (int, error) {
	lAddr0, err := net.ResolveUDPAddr("udp4", "127.0.0.1:0")
	if err != nil {
		return 0, err
	}
	l, err := net.ListenUDP("udp4", lAddr0)
	if err != nil {
		return 0, err
	}
	defer l.Close()
	lAddr := l.LocalAddr()
	lUDPAddr, ok := lAddr.(*net.UDPAddr)
	if !ok {
		return 0, fmt.Errorf("expected *net.UDPAddr, got %v", lAddr)
	}
	port := lUDPAddr.Port
	if port <= 0 {
		return 0, fmt.Errorf("unexpected port %d", port)
	}
	return port, nil
}

View on GitHub (pinned to dd909d0973)

Solutions

  1. Retry freeport.UDP() once.
  2. Ensure no network wrapping/instrumentation is intercepting UDP sockets.
  3. Upgrade Go to a current release to rule out stdlib regressions.
  4. As a workaround, bind manually with net.ListenUDP("udp4", &net.UDPAddr{IP: net.IPv4(127,0,0,1)}) and read the port from the returned *net.UDPAddr.
Defensive patterns

Strategy: try-catch

Type guard

if udpAddr, ok := l.LocalAddr().(*net.UDPAddr); !ok { return 0, fmt.Errorf("expected *net.UDPAddr, got %v", l.LocalAddr()) }

Try / catch

port, err := freeport.UDP()
if err != nil {
    // fallback: manual UDP bind
    conn, lerr := net.ListenUDP("udp4", &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1)})
    if lerr != nil { return fmt.Errorf("freeport.UDP failed: %w", err) }
    defer conn.Close()
    port = conn.LocalAddr().(*net.UDPAddr).Port
}

Prevention

When it happens

Trigger: Calling freeport.UDP() when the PacketConn's LocalAddr() returns a non-UDPAddr implementation, e.g. under unusual network stacks, middleware, or a refactored code path passing a wrapped connection.

Common situations: Rare; seen when running under custom network instrumentation or when Go/OS network behavior changes. Also reproducible with wrapped/mocked PacketConn implementations in tests.

Related errors


AI-assisted analysis of lima-vm/lima@dd909d0973 (2026-09-01). Data as JSON: /api/errors/dfa124dda45d7808. Report an issue: GitHub.