diff --git a/feature/conn25/client.go b/feature/conn25/client.go index dcc8bf5bc..aa35aef59 100644 --- a/feature/conn25/client.go +++ b/feature/conn25/client.go @@ -188,6 +188,10 @@ func (c *client) addTransitIPForConnector(tip netip.Addr, conn tailcfg.NodeView) return c.insertTransitConnMapping(tip, conn.Key()) } +// errQueueFull is returned when the address assignment send queue is full and +// the assignment was dropped. It's backpressure, not a hard failure. +var errQueueFull = errors.New("queue full") + func (c *client) enqueueAddressAssignment(addrs *addrs) error { select { // TODO(fran) investigate the value of waiting for multiple addresses and sending them @@ -196,7 +200,7 @@ func (c *client) enqueueAddressAssignment(addrs *addrs) error { return nil default: c.logf("address assignment queue full, dropping transit assignment for %v", addrs.domain) - return errors.New("queue full") + return errQueueFull } } @@ -341,6 +345,8 @@ func (c *client) lookupTransitIPsByConnKey(k key.NodePublic) ([]netip.Prefix, bo // mapping does not exist on its end. If a mapping is not found on the client // either, this is a no-op. func (c *client) resendTransitIPMapping(transitIP netip.Addr) { + c.mu.Lock() + defer c.mu.Unlock() mapping, ok := c.assignments.lookupByTransitIP(transitIP) if !ok { // We have no mappings for this transit IP, so nothing to resend. diff --git a/feature/conn25/client_test.go b/feature/conn25/client_test.go index 51a4ca4ad..0ca3dbf12 100644 --- a/feature/conn25/client_test.go +++ b/feature/conn25/client_test.go @@ -5,7 +5,9 @@ package conn25 import ( "errors" + "fmt" "net/netip" + "sync" "testing" "time" @@ -498,3 +500,52 @@ func TestAddressExpiryDependsOnActiveFlows(t *testing.T) { }) } } + +// TestResendTransitIPMappingConcurrent races resendTransitIPMapping, against a writer of client.assignments. +func TestResendTransitIPMappingConcurrent(t *testing.T) { + const appName = "a" + conn25 := newConn25(logger.Discard) + c := conn25.client + + // A mapping for the resending goroutine to find, so it takes the found + // path and copies *addrs rather than returning early on the miss. + knownTransit := netip.MustParseAddr("169.254.255.255") + if err := c.assignments.insert(&addrs{ + dst: netip.MustParseAddr("1.2.3.4"), + magic: netip.MustParseAddr("100.64.255.255"), + transit: knownTransit, + app: appName, + domain: "example.com.", + }); err != nil { + t.Fatal(err) + } + + const iters = 2000 + var wg sync.WaitGroup + wg.Go(func() { + for i := range iters { + as := &addrs{ + dst: netip.AddrFrom4([4]byte{10, byte(i >> 8), byte(i), 1}), + magic: netip.AddrFrom4([4]byte{100, 64, byte(i >> 8), byte(i)}), + transit: netip.AddrFrom4([4]byte{169, 254, byte(i >> 8), byte(i)}), + app: appName, + domain: must.Get(dnsname.ToFQDN(fmt.Sprintf("h%d.example.com.", i))), + } + // insert writes the maps resendTransitIPMapping reads, so hold + // c.mu around it exactly as the production writers do. + c.mu.Lock() + err := c.assignments.insert(as) + c.mu.Unlock() + if err != nil { + t.Errorf("insert(%v): %v", as.domain, err) + return + } + } + }) + wg.Go(func() { + for range iters { + c.resendTransitIPMapping(knownTransit) + } + }) + wg.Wait() +}