wgengine/magicsock: prevent errors from marking magicsock as broken

On IPv4-only networks, magicsock receives EHOSTUNREACH whenever it tries
to send out an IPv6 packet. If two EHOSTUNREACH errors were returned 30s
apart, magicsock reported magicsock-receive-func-error to the user. That
error was never cleared because ReadBatch never returned successfully.

SetBroken was meant to raise magicsock-receive-func-error when a receive
func was stuck failing to read. EHOSTUNREACH (and errors like it) should
not raise a warning to the user because they don't indicate a persistent
issue in the receive func.

Remove SetBroken. The receive function still logs paces read errors.

Updates #22871

Change-Id: I33a8362e58207483095d3cb716bb563fcb4be5a0
Signed-off-by: Nick Beirne <nickb@tailscale.com>
This commit is contained in:
Nick Beirne committed 2026-12-04 11:52:08 -05:00
1 parent 82ba6c3623
commit f911c01075
3 files changed
-72

No files matched your search

-28
View File
@@ -1302,8 +1302,6 @@ type ReceiveFuncStats struct {
prevNumCalls uint64
// inCall indicates whether the receive func is currently running.
inCall atomic.Bool
// broken is set by the receive func itself while it cannot read.
broken atomic.Bool
// missing indicates whether the receive func is not running.
missing bool
}
@@ -1328,27 +1326,6 @@ func (s *ReceiveFuncStats) Exit() {
s.inCall.Store(false)
}
// SetBroken records whether the receive func is running but unable to read.
// A broken func is reported like a missing one.
//
// s may be nil.
func (s *ReceiveFuncStats) SetBroken(broken bool) {
if !buildfeatures.HasHealth || s == nil {
return
}
s.broken.Store(broken)
}
// Broken reports the value last set by [ReceiveFuncStats.SetBroken].
//
// s may be nil.
func (s *ReceiveFuncStats) Broken() bool {
if !buildfeatures.HasHealth || s == nil {
return false
}
return s.broken.Load()
}
// ReceiveFuncStats returns the ReceiveFuncStats tracker for the given func
// name, creating it on first use. Repeated calls with the same name return
// the same tracker.
@@ -1376,11 +1353,6 @@ func (t *Tracker) checkReceiveFuncsLocked() {
prev := f.prevNumCalls
numCalls := f.numCalls.Load()
f.prevNumCalls = numCalls
if f.broken.Load() {
// Not OK: the function is running, but not reading.
f.missing = true
continue
}
if numCalls > prev {
// OK: the function has gotten called since last we checked
continue
-18
View File
@@ -1829,10 +1829,6 @@ const (
// readErrRetryDelay paces a [conn.ReceiveFunc]'s read errors.
readErrRetryDelay = 100 * time.Millisecond
// readErrBrokenAfter is how long a [conn.ReceiveFunc] must be failing
// before it reports itself broken to [health.Tracker].
readErrBrokenAfter = 30 * time.Second
)
func (c *Conn) mkReceiveFunc(cfg udpReceiveConfig) conn.ReceiveFunc {
@@ -1841,9 +1837,6 @@ func (c *Conn) mkReceiveFunc(cfg udpReceiveConfig) conn.ReceiveFunc {
directBytesMetric, peerRelayBytesMetric := cfg.directBytes, cfg.peerRelayBytes
name := c.receiveName(cfg.family, cfg.socket)
healthItem := c.health.ReceiveFuncStats(name)
healthItem.SetBroken(false) // in case we inherited health item from a gone Conn
var readErrsSince time.Time
// epCache caches an epAddr->endpoint for hot flows.
var epCache epAddrEndpointCache
@@ -1878,12 +1871,6 @@ func (c *Conn) mkReceiveFunc(cfg udpReceiveConfig) conn.ReceiveFunc {
}
metricRecvUDPError.Add(1)
if readErrsSince.IsZero() {
readErrsSince = time.Now()
}
if time.Since(readErrsSince) >= readErrBrokenAfter {
healthItem.SetBroken(true)
}
c.logf("magicsock: %s: read error: %v", name, err)
c.maybeRebindOnReadError(err)
@@ -1893,11 +1880,6 @@ func (c *Conn) mkReceiveFunc(cfg udpReceiveConfig) conn.ReceiveFunc {
}
continue
}
if !readErrsSince.IsZero() {
c.logf("magicsock: %s: reads recovered after %v", name, time.Since(readErrsSince).Round(time.Millisecond))
readErrsSince = time.Time{}
healthItem.SetBroken(false)
}
reportToCaller := false
for i, batchingPacket := range batchingPackets[:numMsgs] {
-26
View File
@@ -128,29 +128,3 @@ func TestReceiveFuncCloseInterruptsRetryDelay(t *testing.T) {
}
})
}
func TestReceiveFuncBrokenHealth(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
e := readErr(syscall.ECONNRESET)
var script []error
script = append(script, e, e, e, errReadClosed) // call 1: a few errors
// call 2: over readErrBrokenAfter of retries
script = append(script, slices.Repeat([]error{e}, int(readErrBrokenAfter/readErrRetryDelay)+1)...)
script = append(script, errReadClosed, nil, errReadClosed) // call 3: a successful read
c, recvFn := newScriptedRecv(t, &scriptedPacketConn{errors: script})
stats := c.health.ReceiveFuncStats("ReceiveIPv4-test")
for i, want := range []bool{false, true, false} {
recv(recvFn)
if got := stats.Broken(); got != want {
t.Errorf("after call %d: Broken = %v, want %v", i+1, got, want)
}
}
stats.SetBroken(true)
c.mkReceiveFunc(udpReceiveConfig{ruc: &c.pconn4, family: "ReceiveIPv4-test"})
if stats.Broken() {
t.Error("still broken after a new receive func")
}
})
}