Skip to content

Commit 8840285

Browse files
authored
fix natManager to close natManager.nat (#1468)
* rename natManager.natnatmu to follow the standard * fix natManager to close natManager.nat * natmgr: move close to defer in background
1 parent 5a06093 commit 8840285

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

p2p/host/basic/natmgr.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func NewNATManager(net network.Network) NATManager {
3939
// * closing the natManager closes the nat and its mappings.
4040
type natManager struct {
4141
net network.Network
42-
natmu sync.RWMutex
42+
natMx sync.RWMutex
4343
nat *inat.NAT
4444

4545
ready chan struct{} // closed once the nat is ready to process port mappings
@@ -79,6 +79,14 @@ func (nmgr *natManager) Ready() <-chan struct{} {
7979
func (nmgr *natManager) background(ctx context.Context) {
8080
defer nmgr.refCount.Done()
8181

82+
defer func() {
83+
nmgr.natMx.Lock()
84+
if nmgr.nat != nil {
85+
nmgr.nat.Close()
86+
}
87+
nmgr.natMx.Unlock()
88+
}()
89+
8290
discoverCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
8391
defer cancel()
8492
natInstance, err := inat.DiscoverNAT(discoverCtx)
@@ -88,9 +96,9 @@ func (nmgr *natManager) background(ctx context.Context) {
8896
return
8997
}
9098

91-
nmgr.natmu.Lock()
99+
nmgr.natMx.Lock()
92100
nmgr.nat = natInstance
93-
nmgr.natmu.Unlock()
101+
nmgr.natMx.Unlock()
94102
close(nmgr.ready)
95103

96104
// sign natManager up for network notifications
@@ -209,8 +217,8 @@ func (nmgr *natManager) doSync() {
209217
// (a) the search process is still ongoing, or (b) the search process
210218
// found no nat. Clients must check whether the return value is nil.
211219
func (nmgr *natManager) NAT() *inat.NAT {
212-
nmgr.natmu.Lock()
213-
defer nmgr.natmu.Unlock()
220+
nmgr.natMx.Lock()
221+
defer nmgr.natMx.Unlock()
214222
return nmgr.nat
215223
}
216224

0 commit comments

Comments
 (0)