From 6d90e991a3a9bc8a18ce19eba061dc08d3fbd0c0 Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Tue, 1 Nov 2022 19:27:21 +0000 Subject: [PATCH] fix: Flake on `TestReplica/TwentyConcurrent` 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 e3af3786ec92f..76577c8f566bb 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