From 3850b05388a01ac369d78be58a9db04ebb93408c Mon Sep 17 00:00:00 2001 From: Ivan Kozlovic Date: Fri, 22 Apr 2016 17:28:41 -0600 Subject: [PATCH] Fix data race When processing a connect request, there was a risk of race condition when the server was being shutdown. Capture fields that are checked under lock and lock when setting the route's remote ID. Resolves #255 --- server/client.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/server/client.go b/server/client.go index 9eeb9bb7..b9da9c68 100644 --- a/server/client.go +++ b/server/client.go @@ -310,23 +310,28 @@ func (c *client) processConnect(arg []byte) error { c.mu.Lock() c.clearAuthTimer() c.last = time.Now() + typ := c.typ + r := c.route + srv := c.srv c.mu.Unlock() if err := json.Unmarshal(arg, &c.opts); err != nil { return err } - if c.srv != nil { + if srv != nil { // Check for Auth - if ok := c.srv.checkAuth(c); !ok { + if ok := srv.checkAuth(c); !ok { c.authViolation() return ErrAuthorization } } // Grab connection name of remote route. - if c.typ == ROUTER && c.route != nil { + if typ == ROUTER && r != nil { + c.mu.Lock() c.route.remoteID = c.opts.Name + c.mu.Unlock() } if c.opts.Verbose {