From f7d2828f3afbcedbea97ab27010c57fbc85adde3 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sun, 22 Nov 2026 02:25:30 +0000 Subject: [PATCH] wgengine: stamp Status.AsOf when endpoints are read, strictly increasing TestTUNDNS failed on Windows CI with: waitForPeerReachable([uOLy9]): peer [uOLy9]: no endpoints The peer had a HomeDERP in control but never any endpoints, for 30s. userspaceEngine.getStatus snapshotted e.endpoints under e.mu, then walked the wireguard-go peers (which can block on the device mutex), and only then set AsOf: time.Now(). RequestStatus lets two goroutines run getStatus concurrently (for example the magicsock endpoints callback and the TUN "external route: up" event goroutine). If the goroutine that read the old, empty endpoints finished last, its status had the newest AsOf, so LocalBackend.setWgengineStatus's AsOf ordering check accepted it, reset b.endpoints to empty and sent that to control. magicsock's endpoints hadn't changed, so nothing ever resent them. On Windows the monotonic clock is coarse, so two statuses can also get equal AsOf values, which the strict Before check lets through in either order. Take AsOf in the same critical section as the endpoints read and make it strictly increasing, so a status with older endpoints always has an older AsOf and is discarded. Reproduced on Linux by injecting a random 0-20ms sleep in getStatus between the endpoints read and the wgdev peer walk: before: 9 failures in <=48 runs of TestTUNDNS ("no endpoints") after: 0 failures in 56 runs (The natural rate on Linux is too low to measure; the one CI failure was on Windows.) The other TestTUNDNS failure on the dashboard (Linux, TSMP ping "context deadline exceeded") was the wireguard-go duplicate handshake race fixed by the wireguard-go bump in #22537. Fixes #22563 Change-Id: Id290c7422846dc390d3cd914f7b7cfabe593e739 Signed-off-by: Brad Fitzpatrick --- wgengine/userspace.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/wgengine/userspace.go b/wgengine/userspace.go index d748da656..af9e139de 100644 --- a/wgengine/userspace.go +++ b/wgengine/userspace.go @@ -140,6 +140,7 @@ type userspaceEngine struct { closing bool // Close was called (even if we're still closing) statusCallback StatusCallback endpoints []tailcfg.Endpoint + lastStatusAsOf time.Time // AsOf of the last status from getStatus pendOpen map[flowtrackTuple]*pendingOpenFlow // see pendopen.go // pongCallback is the map of response handlers waiting for disco or TSMP @@ -1044,9 +1045,17 @@ func (e *userspaceEngine) getStatus() (*Status, error) { // (See comment in userspaceEngine's declaration.) derpConns := e.magicConn.DERPs() + // Stamp AsOf when endpoints are read, strictly increasing, so the + // status consumer's AsOf ordering check discards any status carrying + // stale endpoints, even with coarse clocks. e.mu.Lock() closing := e.closing localAddrs := slices.Clone(e.endpoints) + asOf := time.Now() + if !asOf.After(e.lastStatusAsOf) { + asOf = e.lastStatusAsOf.Add(1) + } + e.lastStatusAsOf = asOf e.mu.Unlock() if closing { @@ -1072,7 +1081,7 @@ func (e *userspaceEngine) getStatus() (*Status, error) { } return &Status{ - AsOf: time.Now(), + AsOf: asOf, LocalAddrs: localAddrs, Peers: peers, DERPs: derpConns,