copy of https://github.com/OutlineFoundation/tunnel-server/pull/291#292
copy of https://github.com/OutlineFoundation/tunnel-server/pull/291#292ohnorobo wants to merge 2 commits into
Conversation
Greptile SummaryThis PR re-opens and completes the fix from PR #291, addressing a stale-NAT-deletion race in Key changes:
Confidence Score: 4/5PR is safe to merge; the race condition from PR #291 is correctly resolved and well-tested. All three pillars of the fix are mechanically correct: service/udp.go — specifically the Important Files Changed
Sequence DiagramsequenceDiagram
participant ML as Main Loop (goroutine per pkt)
participant NM as natmap
participant A as association
participant HG as Handler Goroutine
Note over ML,HG: Happy path – packet arrives for known live assoc
ML->>NM: Get(clientAddrKey)
NM-->>ML: assoc (existing)
ML->>A: enqueue(pkt) [under mu.Lock]
A-->>ML: queued=true
HG->>A: Read() → receives pkt, calls pkt.done()
Note over ML,HG: Race-safe teardown (DelIfMatches fix)
HG->>A: assocHandle returns → assoc.Close()
A->>A: close(doneCh)
A->>A: mu.Lock → drain readCh → mu.Unlock
HG->>NM: DelIfMatches(key, assoc) [pointer check]
HG->>HG: metrics.RemoveNATEntry()
Note over ML,HG: Next packet from same client after assoc expired
ML->>NM: Get(clientAddrKey)
NM-->>ML: nil (already removed)
ML->>ML: create new assoc A2
ML->>NM: Add(clientAddrKey, A2)
ML->>HG: go assocHandle(ctx, A2)
Note over ML,HG: Concurrent pkt sees closed assoc – enqueue detects it
ML->>A: enqueue(pkt) [under mu.Lock]
A->>A: select {case <-doneCh} → closed!
A-->>ML: queued=false, closed=true
ML->>NM: DelIfMatches(key, assoc) [safe, won't evict A2]
ML->>ML: pkt.done()
Reviews (2): Last reviewed commit: "fix stale NAT deletion race via DelIfMat..." | Re-trigger Greptile |
Address greptile review on #292: both the cleanup goroutine and the main receive loop called nm.Del(clientAddrKey), which could evict a freshly-inserted replacement association for the same client address. Introduce natmap.DelIfMatches so each caller only removes its own association.
Copying closed #291 so I can take another crack at it