]> Cypherpunks repositories - gostls13.git/commitdiff
net: don't wait 5 seconds to re-read /etc/resolv.conf
authorBrad Fitzpatrick <bradfitz@golang.org>
Tue, 27 Jan 2026 17:05:48 +0000 (09:05 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Mon, 2 Feb 2026 18:15:12 +0000 (10:15 -0800)
If a Go process starts up, finds /etc/resolv.conf empty, then the DHCP
client writes /etc/resolv.conf, the Go program would find itself
broken for up to 5 seconds.

We noticed this during integration tests in ephemeral VMs using
gokrazy that boot into our application.

Change-Id: Ia64c2b5c698a4ee3efc15d8a8f1850c47e531b84
Reviewed-on: https://go-review.googlesource.com/c/go/+/739620
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/net/conf_test.go
src/net/dnsclient_unix.go

index 6ebd6be635c95ad7a4379eb2d4d5c82f9574d832..075253cbc13dd9055368adac006f493e4a5945a7 100644 (file)
@@ -396,7 +396,7 @@ func TestConfHostLookupOrder(t *testing.T) {
        defer conf.teardown()
 
        for _, tt := range tests {
-               if !conf.forceUpdateConf(tt.resolv, time.Now().Add(time.Hour)) {
+               if !conf.forceUpdateConf(tt.resolv, distantFuture) {
                        t.Errorf("%s: failed to change resolv config", tt.name)
                }
                for _, ht := range tt.hostTests {
index bf0456ba26cd1bbd5f601179cab3ccd29e583c18..4d7b2fb5ea4de27d9b4a88f01cec4c7bf57026f1 100644 (file)
@@ -382,13 +382,18 @@ func (conf *resolverConfig) init() {
        conf.ch = make(chan struct{}, 1)
 }
 
+// distantFuture is a sentinel time used for tests to signal that
+// resolv.conf should not be rechecked.
+var distantFuture = time.Date(3000, 1, 2, 3, 4, 5, 6, time.UTC)
+
 // tryUpdate tries to update conf with the named resolv.conf file.
 // The name variable only exists for testing. It is otherwise always
 // "/etc/resolv.conf".
 func (conf *resolverConfig) tryUpdate(name string) {
        conf.initOnce.Do(conf.init)
 
-       if conf.dnsConfig.Load().noReload {
+       dc := conf.dnsConfig.Load()
+       if dc.noReload {
                return
        }
 
@@ -399,7 +404,8 @@ func (conf *resolverConfig) tryUpdate(name string) {
        defer conf.releaseSema()
 
        now := time.Now()
-       if conf.lastChecked.After(now.Add(-5 * time.Second)) {
+       if (len(dc.servers) > 0 && conf.lastChecked.After(now.Add(-5*time.Second))) ||
+               conf.lastChecked == distantFuture {
                return
        }
        conf.lastChecked = now