diff --git a/server/client.go b/server/client.go index ba1bbb9c..378897ea 100644 --- a/server/client.go +++ b/server/client.go @@ -578,12 +578,14 @@ func (c *client) clearConnection() { } func (c *client) closeConnection() { + c.mu.Lock() if c.conn == nil { + c.mu.Unlock() return } + Debug("Client connection closed", clientConnStr(c.conn), c.cid) - c.mu.Lock() c.clearAuthTimer() c.clearPingTimer() c.clearConnection() diff --git a/server/client_test.go b/server/client_test.go index 98a81737..43475ee8 100644 --- a/server/client_test.go +++ b/server/client_test.go @@ -418,12 +418,19 @@ func TestClientMapRemoval(t *testing.T) { s, c, _ := setupClient() c.conn.Close() end := time.Now().Add(1 * time.Second) + for time.Now().Before(end) { - if len(s.clients) > 0 { + s.mu.Lock() + lsc := len(s.clients) + s.mu.Unlock() + if lsc > 0 { time.Sleep(5 * time.Millisecond) } } - if len(s.clients) > 0 { + s.mu.Lock() + lsc := len(s.clients) + s.mu.Unlock() + if lsc > 0 { t.Fatal("Client still in server map") } }