From 9c1291997a59b3daecd7b47e8dce0de96497b6b1 Mon Sep 17 00:00:00 2001
From: Ian Luo <ian.luo@gmail.com>
Date: Thu, 13 Aug 2020 13:29:13 +0800
Subject: [PATCH] make sure lock for invokers correctly

---
 cluster/router/chain/chain.go | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/cluster/router/chain/chain.go b/cluster/router/chain/chain.go
index 0ab2c781f..0e9bcd8db 100644
--- a/cluster/router/chain/chain.go
+++ b/cluster/router/chain/chain.go
@@ -147,6 +147,9 @@ func (c *RouterChain) copyRouters() []router.PriorityRouter {
 // copyInvokers copies a snapshot of the received invokers.
 func (c *RouterChain) copyInvokers() []protocol.Invoker {
 	c.mutex.RLock()
+	if c.invokers == nil || len(c.invokers) == 0 {
+		return nil
+	}
 	ret := copySlice(c.invokers)
 	c.mutex.RUnlock()
 	return ret.([]protocol.Invoker)
@@ -171,11 +174,11 @@ func (c *RouterChain) loadCache() *InvokerCache {
 
 // buildCache builds address cache with the new invokers for all poolable routers.
 func (c *RouterChain) buildCache() {
-	if c.invokers == nil || len(c.invokers) == 0 {
+	invokers := c.copyInvokers()
+	if invokers == nil || len(c.invokers) == 0 {
 		return
 	}
 
-	invokers := c.copyInvokers()
 	cache := BuildCache(invokers)
 	origin := c.loadCache()
 
-- 
GitLab