Skip to content
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

fix natManager to close natManager.nat #1468

Merged
merged 3 commits into from
Jun 15, 2022
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
18 changes: 13 additions & 5 deletions p2p/host/basic/natmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,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
Stebalien marked this conversation as resolved.
Show resolved Hide resolved
nat *inat.NAT

ready chan struct{} // closed once the nat is ready to process port mappings
Expand Down Expand Up @@ -77,6 +77,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)
Expand All @@ -86,9 +94,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
Expand Down Expand Up @@ -207,8 +215,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
}

Expand Down