feature/conn25: lock mutext in client resendTransitIPMapping

Lock mutext in resendTransitIPMapping before accessing
the shared resource to prevent data race.

Fixes https://github.com/tailscale/corp/issues/49568

Change-Id: I35b9e99428361732c9a785020d7609d29a99877e
Signed-off-by: Mazdak Nasab <[email protected]>
This commit is contained in:
Mazdak Nasab committed 2026-10-08 09:26:56 -07:00
1 parent a89b32904f
commit 3420967a5f
2 files changed
+58 -1

No files matched your search

+7 -1
View File
@@ -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.
+51
View File
@@ -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()
}