From 980b7728013567bf9cea0af5ffd7b2fdc07252a5 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sun, 26 Apr 2020 17:41:36 +0300 Subject: [PATCH] fix races in web and logger (#1576) Signed-off-by: Vasiliy Tolstov --- logger/default.go | 7 ++++++- web/service.go | 11 ++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/logger/default.go b/logger/default.go index 6b3f4652..e027502e 100644 --- a/logger/default.go +++ b/logger/default.go @@ -136,7 +136,12 @@ func (l *defaultLogger) Logf(level Level, format string, v ...interface{}) { } func (n *defaultLogger) Options() Options { - return n.opts + // not guard against options Context values + n.RLock() + opts := n.opts + opts.Fields = copyFields(n.opts.Fields) + n.RUnlock() + return opts } // NewLogger builds a new logger based on options diff --git a/web/service.go b/web/service.go index 81895f08..62e0a31e 100644 --- a/web/service.go +++ b/web/service.go @@ -29,7 +29,7 @@ type service struct { mux *http.ServeMux srv *registry.Service - sync.Mutex + sync.RWMutex running bool static bool exit chan chan error @@ -90,11 +90,14 @@ func (s *service) genSrv() *registry.Service { } func (s *service) run(exit chan bool) { + s.RLock() if s.opts.RegisterInterval <= time.Duration(0) { + s.RUnlock() return } t := time.NewTicker(s.opts.RegisterInterval) + s.RUnlock() for { select { @@ -327,6 +330,9 @@ func (s *service) HandleFunc(pattern string, handler func(http.ResponseWriter, * } func (s *service) Init(opts ...Option) error { + s.Lock() + defer s.Unlock() + for _, o := range opts { o(&s.opts) } @@ -342,6 +348,9 @@ func (s *service) Init(opts ...Option) error { } serviceOpts = append(serviceOpts, micro.Action(func(ctx *cli.Context) error { + s.Lock() + defer s.Unlock() + if ttl := ctx.Int("register_ttl"); ttl > 0 { s.opts.RegisterTTL = time.Duration(ttl) * time.Second }