diff --git a/p2p/host/basic/natmgr.go b/p2p/host/basic/natmgr.go index dc6b156552..120b5d1099 100644 --- a/p2p/host/basic/natmgr.go +++ b/p2p/host/basic/natmgr.go @@ -39,7 +39,7 @@ func NewNATManager(net network.Network) NATManager { // * closing the natManager closes the nat and its mappings. type natManager struct { net network.Network - natmu sync.RWMutex + natMx sync.RWMutex nat *inat.NAT ready chan struct{} // closed once the nat is ready to process port mappings @@ -79,6 +79,14 @@ func (nmgr *natManager) Ready() <-chan struct{} { func (nmgr *natManager) background(ctx context.Context) { defer nmgr.refCount.Done() + defer func() { + nmgr.natMx.Lock() + if nmgr.nat != nil { + nmgr.nat.Close() + } + nmgr.natMx.Unlock() + }() + discoverCtx, cancel := context.WithTimeout(ctx, 10*time.Second) defer cancel() natInstance, err := inat.DiscoverNAT(discoverCtx) @@ -88,9 +96,9 @@ func (nmgr *natManager) background(ctx context.Context) { return } - nmgr.natmu.Lock() + nmgr.natMx.Lock() nmgr.nat = natInstance - nmgr.natmu.Unlock() + nmgr.natMx.Unlock() close(nmgr.ready) // sign natManager up for network notifications @@ -209,8 +217,8 @@ func (nmgr *natManager) doSync() { // (a) the search process is still ongoing, or (b) the search process // found no nat. Clients must check whether the return value is nil. func (nmgr *natManager) NAT() *inat.NAT { - nmgr.natmu.Lock() - defer nmgr.natmu.Unlock() + nmgr.natMx.Lock() + defer nmgr.natMx.Unlock() return nmgr.nat }