fix race condition in holepunch service
@@ -47,6 +47,8 @@ type Service struct {
host host.Host
ids identify.IDService
+
+ holePuncherMx sync.Mutex
holePuncher *holePuncher
hasPublicAddrsChan chan struct{}
@@ -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
}
@@ -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()
@@ -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)
}
