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 <[email protected]>
This commit is contained in:
Brad Fitzpatrick authored and Brad Fitzpatrick committed 2026-11-22 22:12:16 -08:00
1 parent ceccbc303d
commit f7d2828f3a
1 file changed
+10 -1
+10 -1
View File
@@ -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,