Skip to content

Commit

Permalink
fixed race condition in accessing mdnsService.server
Browse files Browse the repository at this point in the history
  • Loading branch information
MOHANKUMAR-IT committed Dec 10, 2024
1 parent b1fdc89 commit 9ee3a1d
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions p2p/discovery/mdns/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ type mdnsService struct {
server *zeroconf.Server

notifee Notifee

mu sync.Mutex
}

func NewMdnsService(host host.Host, serviceName string, notifee Notifee) *mdnsService {
Expand Down Expand Up @@ -90,13 +92,15 @@ func (s *mdnsService) Start() error {
return
case evt := <-ipEvt.Out():
if _, ok := evt.(event.EvtLocalAddressesUpdated); ok {
s.mu.Lock()
if s.server != nil {
s.server.Shutdown()
s.server = nil
}
if err = s.startServer(); err != nil {
log.Errorf("failed to restart mdns server: %s", err)
}
s.mu.Unlock()
}
}
}
Expand All @@ -106,6 +110,9 @@ func (s *mdnsService) Start() error {
}

func (s *mdnsService) Close() error {
s.mu.Lock()
defer s.mu.Unlock()

s.ctxCancel()
if s.server != nil {
s.server.Shutdown()
Expand Down

0 comments on commit 9ee3a1d

Please sign in to comment.