Skip to content

Commit 1f63dc4

Browse files
beiwei30chickenlj
authored andcommitted
Merge pull request apache#1836, there is a potential deadlock in DubboProtocol#getSharedClient.
Fixes apache#677.
1 parent 404408b commit 1f63dc4

File tree

1 file changed

+9
-1
lines changed
  • dubbo-rpc/dubbo-rpc-dubbo/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo

1 file changed

+9
-1
lines changed

dubbo-rpc/dubbo-rpc-dubbo/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DubboProtocol.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public class DubboProtocol extends AbstractProtocol {
6565
private final Map<String, ExchangeServer> serverMap = new ConcurrentHashMap<String, ExchangeServer>(); // <host:port,Exchanger>
6666
private final Map<String, ReferenceCountExchangeClient> referenceClientMap = new ConcurrentHashMap<String, ReferenceCountExchangeClient>(); // <host:port,Exchanger>
6767
private final ConcurrentMap<String, LazyConnectExchangeClient> ghostClientMap = new ConcurrentHashMap<String, LazyConnectExchangeClient>();
68+
private final ConcurrentMap<String, Object> locks = new ConcurrentHashMap<String, Object>();
6869
private final Set<String> optimizers = new ConcurrentHashSet<String>();
6970
//consumer side export a stub service for dispatching event
7071
//servicekey-stubmethods
@@ -373,11 +374,18 @@ private ExchangeClient getSharedClient(URL url) {
373374
referenceClientMap.remove(key);
374375
}
375376
}
376-
synchronized (key.intern()) {
377+
378+
locks.putIfAbsent(key, new Object());
379+
synchronized (locks.get(key)) {
380+
if (referenceClientMap.containsKey(key)) {
381+
return referenceClientMap.get(key);
382+
}
383+
377384
ExchangeClient exchangeClient = initClient(url);
378385
client = new ReferenceCountExchangeClient(exchangeClient, ghostClientMap);
379386
referenceClientMap.put(key, client);
380387
ghostClientMap.remove(key);
388+
locks.remove(key);
381389
return client;
382390
}
383391
}

0 commit comments

Comments
 (0)