From 288e7d10455e3d09f0b573d80586a796e683dec6 Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Tue, 1 Nov 2022 13:28:34 -0700 Subject: [PATCH] fix: Flake on `TestReplica/TwentyConcurrent` (#4842) This could actually cause connections to intermittently fail too when a CPU is absolutely pegged. It just so happens that only our runners have been that slow! Fixes #4607. --- tailnet/conn.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tailnet/conn.go b/tailnet/conn.go index e3af3786ec..76577c8f56 100644 --- a/tailnet/conn.go +++ b/tailnet/conn.go @@ -216,7 +216,18 @@ func NewConn(options *Options) (*Conn, error) { server.sendNode() }) wireguardEngine.SetNetInfoCallback(func(ni *tailcfg.NetInfo) { + server.logger.Info(context.Background(), "netinfo callback", slog.F("netinfo", ni)) + // If the lastMutex is blocked, it's possible that + // multiple NetInfo callbacks occur at the same time. + // + // We need to ensure only the latest is sent! + asOf := time.Now() server.lastMutex.Lock() + if asOf.Before(server.lastNetInfo) { + server.lastMutex.Unlock() + return + } + server.lastNetInfo = asOf server.lastPreferredDERP = ni.PreferredDERP server.lastDERPLatency = ni.DERPLatency server.lastMutex.Unlock() @@ -269,6 +280,7 @@ type Conn struct { // It's only possible to store these values via status functions, // so the values must be stored for retrieval later on. lastStatus time.Time + lastNetInfo time.Time lastEndpoints []string lastPreferredDERP int lastDERPLatency map[string]float64