Skip to content

fix(net/nat): data race problem of extAddr #3140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions p2p/net/nat/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/netip"
"sync"
"sync/atomic"
"time"

logging "github.com/ipfs/go-log/v2"
Expand Down Expand Up @@ -56,11 +57,11 @@ func DiscoverNAT(ctx context.Context) (*NAT, error) {
ctx, cancel := context.WithCancel(context.Background())
nat := &NAT{
nat: natInstance,
extAddr: extAddr,
mappings: make(map[entry]int),
ctx: ctx,
ctxCancel: cancel,
}
nat.extAddr.Store(&extAddr)
nat.refCount.Add(1)
go func() {
defer nat.refCount.Done()
Expand All @@ -77,7 +78,7 @@ type NAT struct {
natmu sync.Mutex
nat nat.NAT
// External IP of the NAT. Will be renewed periodically (every CacheTime).
extAddr netip.Addr
extAddr atomic.Pointer[netip.Addr]

refCount sync.WaitGroup
ctx context.Context
Expand All @@ -103,15 +104,15 @@ func (nat *NAT) GetMapping(protocol string, port int) (addr netip.AddrPort, foun
nat.mappingmu.Lock()
defer nat.mappingmu.Unlock()

if !nat.extAddr.IsValid() {
if !nat.extAddr.Load().IsValid() {
return netip.AddrPort{}, false
}
extPort, found := nat.mappings[entry{protocol: protocol, port: port}]
// The mapping may have an invalid port.
if !found || extPort == 0 {
return netip.AddrPort{}, false
}
return netip.AddrPortFrom(nat.extAddr, uint16(extPort)), true
return netip.AddrPortFrom(*nat.extAddr.Load(), uint16(extPort)), true
}

// AddMapping attempts to construct a mapping on protocol and internal port.
Expand Down Expand Up @@ -206,7 +207,7 @@ func (nat *NAT) background() {
if err == nil {
extAddr, _ = netip.AddrFromSlice(extIP)
}
nat.extAddr = extAddr
nat.extAddr.Store(&extAddr)
nextAddrUpdate = time.Now().Add(CacheTime)
}
t.Reset(time.Until(minTime(nextAddrUpdate, nextMappingUpdate)))
Expand Down
Loading