Skip to content

Commit

Permalink
fix race condition causing TestAutoNATServiceDialError test failure
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Feb 6, 2022
1 parent f2fa0b6 commit 9df99b3
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions p2p/host/autonat/svc.go
Expand Up @@ -242,6 +242,7 @@ func (as *autoNATService) Enable() {
ctx, cancel := context.WithCancel(context.Background())
as.instance = cancel
as.backgroundRunning = make(chan struct{})
as.config.host.SetStreamHandler(AutoNATProto, as.handleStream)

go as.background(ctx)
}
Expand All @@ -251,6 +252,7 @@ func (as *autoNATService) Disable() {
as.instanceLock.Lock()
defer as.instanceLock.Unlock()
if as.instance != nil {
as.config.host.RemoveStreamHandler(AutoNATProto)
as.instance()
as.instance = nil
<-as.backgroundRunning
Expand All @@ -259,7 +261,6 @@ func (as *autoNATService) Disable() {

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

timer := time.NewTimer(as.config.throttleResetPeriod)
defer timer.Stop()
Expand All @@ -274,7 +275,6 @@ func (as *autoNATService) background(ctx context.Context) {
jitter := rand.Float32() * float32(as.config.throttleResetJitter)
timer.Reset(as.config.throttleResetPeriod + time.Duration(int64(jitter)))
case <-ctx.Done():
as.config.host.RemoveStreamHandler(AutoNATProto)
return
}
}
Expand Down

0 comments on commit 9df99b3

Please sign in to comment.