File tree 1 file changed +13
-5
lines changed
1 file changed +13
-5
lines changed Original file line number Diff line number Diff line change @@ -39,7 +39,7 @@ func NewNATManager(net network.Network) NATManager {
39
39
// * closing the natManager closes the nat and its mappings.
40
40
type natManager struct {
41
41
net network.Network
42
- natmu sync.RWMutex
42
+ natMx sync.RWMutex
43
43
nat * inat.NAT
44
44
45
45
ready chan struct {} // closed once the nat is ready to process port mappings
@@ -79,6 +79,14 @@ func (nmgr *natManager) Ready() <-chan struct{} {
79
79
func (nmgr * natManager ) background (ctx context.Context ) {
80
80
defer nmgr .refCount .Done ()
81
81
82
+ defer func () {
83
+ nmgr .natMx .Lock ()
84
+ if nmgr .nat != nil {
85
+ nmgr .nat .Close ()
86
+ }
87
+ nmgr .natMx .Unlock ()
88
+ }()
89
+
82
90
discoverCtx , cancel := context .WithTimeout (ctx , 10 * time .Second )
83
91
defer cancel ()
84
92
natInstance , err := inat .DiscoverNAT (discoverCtx )
@@ -88,9 +96,9 @@ func (nmgr *natManager) background(ctx context.Context) {
88
96
return
89
97
}
90
98
91
- nmgr .natmu .Lock ()
99
+ nmgr .natMx .Lock ()
92
100
nmgr .nat = natInstance
93
- nmgr .natmu .Unlock ()
101
+ nmgr .natMx .Unlock ()
94
102
close (nmgr .ready )
95
103
96
104
// sign natManager up for network notifications
@@ -209,8 +217,8 @@ func (nmgr *natManager) doSync() {
209
217
// (a) the search process is still ongoing, or (b) the search process
210
218
// found no nat. Clients must check whether the return value is nil.
211
219
func (nmgr * natManager ) NAT () * inat.NAT {
212
- nmgr .natmu .Lock ()
213
- defer nmgr .natmu .Unlock ()
220
+ nmgr .natMx .Lock ()
221
+ defer nmgr .natMx .Unlock ()
214
222
return nmgr .nat
215
223
}
216
224
You can’t perform that action at this time.
0 commit comments