Skip to content

Commit e62f2d6

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 99b45e0 commit e62f2d6

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

go/libkb/client.go

+9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"time"
1818

1919
"github.com/keybase/go-framed-msgpack-rpc/rpc"
20+
"github.com/keybase/go-framed-msgpack-rpc/rpc/resinit"
2021

2122
"h12.me/socks"
2223
)
@@ -146,6 +147,13 @@ func NewClient(e *Env, config *ClientConfig, needCookie bool) *Client {
146147
xprt.Dial = func(network, addr string) (c net.Conn, err error) {
147148
c, err = net.Dial(network, addr)
148149
if err != nil {
150+
// If we get a DNS error, it could be because glibc has cached an
151+
// old version of /etc/resolv.conf. The res_init() libc function
152+
// busts that cache and keeps us from getting stuck in a state
153+
// where DNS requests keep failing even though the network is up.
154+
// This is similar to what the Rust standard library does:
155+
// https://github.com/rust-lang/rust/blob/028569ab1b/src/libstd/sys_common/net.rs#L186-L190
156+
resinit.ResInitIfDNSError(err)
149157
return c, err
150158
}
151159
if err = rpc.DisableSigPipe(c); err != nil {
@@ -159,6 +167,7 @@ func NewClient(e *Env, config *ClientConfig, needCookie bool) *Client {
159167
xprt.TLSClientConfig = &tls.Config{RootCAs: config.RootCAs}
160168
}
161169
if e.GetTorMode().Enabled() {
170+
// TODO: should we call res_init on DNS errors here as well?
162171
dialSocksProxy := socks.DialSocksProxy(socks.SOCKS5, e.GetTorProxy())
163172
xprt.Dial = dialSocksProxy
164173
} else {

0 commit comments

Comments
 (0)