Skip to content

Commit 618e985

Browse files
committed
Add start stop mutex
1 parent dd1c1ed commit 618e985

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

internal/mdns/mdns.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"fmt"
99
"net"
10+
"sync"
1011
"time"
1112

1213
"github.com/libp2p/go-libp2p-core/peer"
@@ -36,9 +37,11 @@ type Service struct {
3637
server *mdns.Server
3738

3839
// Internal service management fields.
39-
started bool
40-
stop chan struct{}
41-
done chan struct{}
40+
// startStopMutex is to prevent concurrent calls to Start and Stop.
41+
startStopMutex sync.Mutex
42+
started bool
43+
stop chan struct{}
44+
done chan struct{}
4245
}
4346

4447
// NewService creates and returns a new mDNS service.
@@ -59,6 +62,9 @@ func NewService(p2pHost IDNetworker, serviceTag string,
5962

6063
// Start starts the mDNS service.
6164
func (s *Service) Start() (err error) {
65+
s.startStopMutex.Lock()
66+
defer s.startStopMutex.Unlock()
67+
6268
if s.started {
6369
return nil
6470
}
@@ -96,6 +102,9 @@ func (s *Service) Start() (err error) {
96102

97103
// Stop stops the mDNS service and server.
98104
func (s *Service) Stop() (err error) {
105+
s.startStopMutex.Lock()
106+
defer s.startStopMutex.Unlock()
107+
99108
if !s.started {
100109
return nil
101110
}

0 commit comments

Comments
 (0)