Skip to content

Commit 9a3cbac

Browse files
committed
call libc::res_init() in response to DNS failures
Go's DNS resolution often defers to the libc implementation, and glibc's resolver has a serious bug: https://sourceware.org/bugzilla/show_bug.cgi?id=984 It will cache the contents of /etc/resolv.conf, which can put the client in a state where all DNS requests fail forever after a network change. The conditions where Go calls into libc are complicated and platform-specific, and the resolver cache involves thread-local state, so repros tend to be inconsistent. But when you hit this on your laptop on the subway or whatever, the effect is that everything is broken until you restart the process. One way to fix this would be to force using the pure-Go resolver (net.DefaultResolver.PreferGo = true), which refreshes /etc/resolv.conf every 5 seconds. I'm wary of doing that, because the Go devs went through an enormous amount of trouble to enable cgo fallback, for various platform- and environment-specific reasons. See all the comments in net/conf.go::initConfVal() and net/conf.go::hostLookupOrder() in the standard library. Instead, we're trying the same workaround that the Rust standard library chose, where we call libc::res_init() after DNS failures. See rust-lang/rust#41570. The downside here is that we have to remember to do this after we make network calls, and that we have to use cgo in the build, but the upside is that it should never break a DNS environment that was working before.
1 parent 6c50f8c commit 9a3cbac

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

rpc/connection.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
"github.com/keybase/backoff"
15+
"github.com/keybase/go-framed-msgpack-rpc/rpc/resinit"
1516
"golang.org/x/net/context"
1617
)
1718

@@ -73,6 +74,15 @@ func (t *connTransport) Dial(context.Context) (Transporter, error) {
7374
var err error
7475
t.conn, err = t.uri.Dial()
7576
if err != nil {
77+
// If we get a DNS error, it could be because glibc has cached an old
78+
// version of /etc/resolv.conf. The res_init() libc function busts that
79+
// cache and keeps us from getting stuck in a state where DNS requests
80+
// keep failing even though the network is up. This is similar to what
81+
// the Rust standard library does:
82+
// https://github.com/rust-lang/rust/blob/028569ab1b/src/libstd/sys_common/net.rs#L186-L190
83+
// Note that we still propagate the error here, and we expect callers
84+
// to retry.
85+
resinit.ResInitIfDNSError(err)
7686
return nil, err
7787
}
7888

rpc/resinit/resinit.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package resinit
2+
3+
import "net"
4+
5+
// If we get a DNS error, it could be because glibc has cached an old version
6+
// of /etc/resolv.conf. The res_init() libc function busts that cache and keeps
7+
// us from getting stuck in a state where DNS requests keep failing even though
8+
// the network is up. This is similar to what the Rust standard library does:
9+
// https://github.com/rust-lang/rust/blob/028569ab1b/src/libstd/sys_common/net.rs#L186-L190
10+
func ResInitIfDNSError(err error) {
11+
// There are two error cases we need to handle, a raw *net.DNSError, and
12+
// one wrapped in a *net.OpError. Detect that second case, and unwrap it.
13+
if opErr, isOpErr := err.(*net.OpError); isOpErr {
14+
err = opErr.Err
15+
}
16+
if _, isDNSError := err.(*net.DNSError); isDNSError {
17+
// defined per platform in resinit_*.go
18+
resInit()
19+
}
20+
}

rpc/resinit/resinit_android.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
2+
// this source code is governed by the included BSD license.
3+
4+
// +build android
5+
6+
package resinit
7+
8+
// /* Bionic has res_init() but it's not in any header. Patterned off of: */
9+
// /* https://mail.gnome.org/archives/commits-list/2013-May/msg01329.html */
10+
// int res_init (void);
11+
import "C"
12+
13+
func resInit() {
14+
C.res_init()
15+
}

rpc/resinit/resinit_nix.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
2+
// this source code is governed by the included BSD license.
3+
4+
// +build !windows,!android
5+
6+
package resinit
7+
8+
// #cgo LDFLAGS: -lresolv
9+
// #include<resolv.h>
10+
import "C"
11+
12+
func resInit() {
13+
C.res_init()
14+
}

rpc/resinit/resinit_windows.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
2+
// this source code is governed by the included BSD license.
3+
4+
// +build windows
5+
6+
package resinit
7+
8+
func resInit() {
9+
// no-op
10+
}

0 commit comments

Comments
 (0)