Open chriso opened 1 year ago
If I replace the hardcoded DialUDP
call with r.dial("udp")
then the provided Dial
function is used in all cases.
-c, err = DialUDP("udp", nil, &dst)
+c, err = r.dial(ctx, "udp", dst.IP.String())
This has the additional benefit of threading the lookup context through to the underlying dialer.
If we're concerned about breaking code in the wild, we could instead opt-in by target, and take this path for GOOS=wasip1
only for now (since it has limited networking capabilities, and DialUDP
always fails).
This approach was suggested by @mateusz834:
if runtime.GOOS == "wasip1" {
c, err = r.dial(ctx, "udp", dst.IP.String())
} else {
c, err = DialUDP("udp", nil, &dst)
}
@ianlancetaylor suggested that we might instead require an additional hook:
type Resolver struct {
Dial func(ctx context.Context, network, address string) (Conn, error)
// Extra hook:
DialUDP func(ctx context.Context, network, address string) (Conn, error)
}
or something like this:
type Resolver struct {
Dial func(ctx context.Context, network, address string) (Conn, error)
// Extra hook:
UDPConnect func(ctx context.Context, *UDPAddr) (*UDPAddr, bool)
}
Change https://go.dev/cl/500576 mentions this issue: net: prefer Resolver.Dial over DialUDP on wasip1
The runtime.GOOS == "wasip1"
guard was just a simple fix idea, but I agree with @ianlancetaylor that having a per platform behaviour in this case is not ideal.
I think that this hook should be named something like IsAddrReachable
, so that the intention is clear.
And probably it should use the netip.Addr at this point.
type Resolver struct {
// IsAddrReachable is used for address sorting by the go resolver.
// When this field is equal to nil, the default dialer is being used. addr is considered reachable,
// when the default dialer sucesfully establishes a UDP connection to addr.
IsAddrReachable func(ctx context.Context, addr netip.Addr) (local netip.Addr, reachable bool)
}
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
The
net.Resolver
accepts an optionalDial
function that says the following:I created a script that logs
Dial
calls when using the pure Go resolver: https://go.dev/play/p/0O_ARZyK2eGIf I run this script locally, I see something like this:
However, if I run the script with strace, I see that Go is making additional connections some other way:
There's is one hardcoded call to
net.DialUDP
here which appears to be the source of the additional connections.What did you expect to see?
I expect to see the
Dial
function used for all connections made by the pure Go resolver.What did you see instead?
I see that the
Dial
function is only used in some cases.Additional context
CL 500576 fixes the issue by using
net.Resolver.Dial
in all cases.For context, this change is important for targets with limited networking capabilities (e.g.
GOOS=wasip1
). It means that users can provide their ownDial
function to make use of the pure Go resolver. At the moment the hardcodednet.DialUDP
call makes the pure Go resolver off limits for these targets.There was some concern in the CL about whether making this change for all targets would break code in the wild. I'm submitting it as a bug report so we can discuss here instead.
cc
GOOS=wasip1
maintainers: @achille-roussel @johanbrandhorst @Pryzcc those that commented on CL 500576: @mateusz834 @ianlancetaylor