From 17671de22626f84eb86338dc9a5120b5d8d55ab2 Mon Sep 17 00:00:00 2001 From: beiwei30 Date: Wed, 23 May 2018 16:41:32 +0800 Subject: [PATCH 1/2] #677: There is a potential deadlock in DubboProtocol#getSharedClient --- .../com/alibaba/dubbo/rpc/protocol/dubbo/DubboProtocol.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboProtocol.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboProtocol.java index 4e8b971328d..1fab24caa6d 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboProtocol.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboProtocol.java @@ -373,7 +373,7 @@ private ExchangeClient getSharedClient(URL url) { referenceClientMap.remove(key); } } - synchronized (key.intern()) { + synchronized ((key + ".lock").intern()) { ExchangeClient exchangeClient = initClient(url); client = new ReferenceCountExchangeClient(exchangeClient, ghostClientMap); referenceClientMap.put(key, client); From 6a8424f67819f2b55cc36db255841433e7a23190 Mon Sep 17 00:00:00 2001 From: beiwei30 Date: Thu, 24 May 2018 10:15:38 +0800 Subject: [PATCH 2/2] #677: There is a potential deadlock in DubboProtocol#getSharedClient --- .../dubbo/rpc/protocol/dubbo/DubboProtocol.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboProtocol.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboProtocol.java index 1fab24caa6d..017e4a93df3 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboProtocol.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboProtocol.java @@ -65,6 +65,7 @@ public class DubboProtocol extends AbstractProtocol { private final Map serverMap = new ConcurrentHashMap(); // private final Map referenceClientMap = new ConcurrentHashMap(); // private final ConcurrentMap ghostClientMap = new ConcurrentHashMap(); + private final ConcurrentMap locks = new ConcurrentHashMap(); private final Set optimizers = new ConcurrentHashSet(); //consumer side export a stub service for dispatching event //servicekey-stubmethods @@ -373,11 +374,18 @@ private ExchangeClient getSharedClient(URL url) { referenceClientMap.remove(key); } } - synchronized ((key + ".lock").intern()) { + + locks.putIfAbsent(key, new Object()); + synchronized (locks.get(key)) { + if (referenceClientMap.containsKey(key)) { + return referenceClientMap.get(key); + } + ExchangeClient exchangeClient = initClient(url); client = new ReferenceCountExchangeClient(exchangeClient, ghostClientMap); referenceClientMap.put(key, client); ghostClientMap.remove(key); + locks.remove(key); return client; } }