Skip to content

Commit 4ab6503

Browse files
committed
fix data race in case of unregister services
1 parent 091fbb0 commit 4ab6503

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

โ€Žserver/server.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ type Server struct {
126126
ServerErrorFunc func(res *protocol.Message, err error) string
127127

128128
// The server is started.
129-
Started chan struct{}
129+
Started chan struct{}
130+
unregisterAllOnce sync.Once
130131
}
131132

132133
// NewServer returns a server.
@@ -955,11 +956,8 @@ func (s *Server) Shutdown(ctx context.Context) error {
955956
s.mu.Lock()
956957

957958
// ไธปๅŠจๆณจ้”€ๆณจๅ†Œ็š„ๆœๅŠก
958-
if s.Plugins != nil {
959-
for name := range s.serviceMap {
960-
s.Plugins.DoUnregister(name)
961-
}
962-
}
959+
s.UnregisterAll()
960+
963961
if s.ln != nil {
964962
s.ln.Close()
965963
}

โ€Žserver/service.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,12 @@ func suitableMethods(typ reflect.Type, reportErr bool) map[string]*methodType {
325325
// UnregisterAll unregisters all services.
326326
// You can call this method when you want to shutdown/upgrade this node.
327327
func (s *Server) UnregisterAll() error {
328-
return s.unregisterAll()
328+
var err error
329+
s.unregisterAllOnce.Do(func() {
330+
err = s.unregisterAll()
331+
})
332+
333+
return err
329334
}
330335

331336
func (s *Server) unregisterAll() error {

0 commit comments

Comments
ย (0)