Skip to content

Commit

Permalink
Merge pull request #1474 from libp2p/fix-holepunch-service-backport
Browse files Browse the repository at this point in the history
fix race condition in holepunch service, release v0.19.1
  • Loading branch information
marten-seemann committed May 2, 2022
2 parents 0d51d87 + 2bacab0 commit 4cabcba
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
22 changes: 17 additions & 5 deletions p2p/protocol/holepunch/svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ type Service struct {
ctx context.Context
ctxCancel context.CancelFunc

host host.Host
ids identify.IDService
holePuncher *holePuncher
host host.Host
ids identify.IDService

holePuncherMx sync.Mutex
holePuncher *holePuncher

hasPublicAddrsChan chan struct{}

Expand Down Expand Up @@ -138,7 +140,9 @@ func (s *Service) watchForPublicAddr() {
if e.(event.EvtLocalReachabilityChanged).Reachability != network.ReachabilityPrivate {
continue
}
s.holePuncherMx.Lock()
s.holePuncher = newHolePuncher(s.host, s.ids, s.tracer)
s.holePuncherMx.Unlock()
close(s.hasPublicAddrsChan)
return
}
Expand All @@ -147,7 +151,12 @@ func (s *Service) watchForPublicAddr() {

// Close closes the Hole Punch Service.
func (s *Service) Close() error {
err := s.holePuncher.Close()
var err error
s.holePuncherMx.Lock()
if s.holePuncher != nil {
err = s.holePuncher.Close()
}
s.holePuncherMx.Unlock()
s.tracer.Close()
s.host.RemoveStreamHandler(Protocol)
s.ctxCancel()
Expand Down Expand Up @@ -257,5 +266,8 @@ func (s *Service) handleNewStream(str network.Stream) {
// TODO: find a solution for this.
func (s *Service) DirectConnect(p peer.ID) error {
<-s.hasPublicAddrsChan
return s.holePuncher.DirectConnect(p)
s.holePuncherMx.Lock()
holePuncher := s.holePuncher
s.holePuncherMx.Unlock()
return holePuncher.DirectConnect(p)
}
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "v0.19.0"
"version": "v0.19.1"
}

0 comments on commit 4cabcba

Please sign in to comment.