Skip to content

Commit 807e6b0

Browse files
fix race condition causing TestAutoNATServiceDialError test failure (#1312)
1 parent cfbe41a commit 807e6b0

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

p2p/host/autonat/svc.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ func (as *autoNATService) Enable() {
242242
ctx, cancel := context.WithCancel(context.Background())
243243
as.instance = cancel
244244
as.backgroundRunning = make(chan struct{})
245+
as.config.host.SetStreamHandler(AutoNATProto, as.handleStream)
245246

246247
go as.background(ctx)
247248
}
@@ -251,6 +252,7 @@ func (as *autoNATService) Disable() {
251252
as.instanceLock.Lock()
252253
defer as.instanceLock.Unlock()
253254
if as.instance != nil {
255+
as.config.host.RemoveStreamHandler(AutoNATProto)
254256
as.instance()
255257
as.instance = nil
256258
<-as.backgroundRunning
@@ -259,7 +261,6 @@ func (as *autoNATService) Disable() {
259261

260262
func (as *autoNATService) background(ctx context.Context) {
261263
defer close(as.backgroundRunning)
262-
as.config.host.SetStreamHandler(AutoNATProto, as.handleStream)
263264

264265
timer := time.NewTimer(as.config.throttleResetPeriod)
265266
defer timer.Stop()
@@ -274,7 +275,6 @@ func (as *autoNATService) background(ctx context.Context) {
274275
jitter := rand.Float32() * float32(as.config.throttleResetJitter)
275276
timer.Reset(as.config.throttleResetPeriod + time.Duration(int64(jitter)))
276277
case <-ctx.Done():
277-
as.config.host.RemoveStreamHandler(AutoNATProto)
278278
return
279279
}
280280
}

0 commit comments

Comments
 (0)