Skip to content

Commit

Permalink
fix data race in case of unregister services
Browse files Browse the repository at this point in the history
  • Loading branch information
smallnest committed Aug 22, 2024
1 parent 091fbb0 commit 4ab6503
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
10 changes: 4 additions & 6 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ type Server struct {
ServerErrorFunc func(res *protocol.Message, err error) string

// The server is started.
Started chan struct{}
Started chan struct{}
unregisterAllOnce sync.Once
}

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

// ไธปๅŠจๆณจ้”€ๆณจๅ†Œ็š„ๆœๅŠก
if s.Plugins != nil {
for name := range s.serviceMap {
s.Plugins.DoUnregister(name)
}
}
s.UnregisterAll()

if s.ln != nil {
s.ln.Close()
}
Expand Down
7 changes: 6 additions & 1 deletion server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,12 @@ func suitableMethods(typ reflect.Type, reportErr bool) map[string]*methodType {
// UnregisterAll unregisters all services.
// You can call this method when you want to shutdown/upgrade this node.
func (s *Server) UnregisterAll() error {
return s.unregisterAll()
var err error
s.unregisterAllOnce.Do(func() {
err = s.unregisterAll()
})

return err
}

func (s *Server) unregisterAll() error {
Expand Down

0 comments on commit 4ab6503

Please sign in to comment.