Skip to content

Commit

Permalink
Merge pull request apache#1836, there is a potential deadlock in Dubb…
Browse files Browse the repository at this point in the history
…oProtocol#getSharedClient.

Fixes apache#677.
  • Loading branch information
beiwei30 authored and chickenlj committed May 25, 2018
1 parent 404408b commit 1f63dc4
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class DubboProtocol extends AbstractProtocol {
private final Map<String, ExchangeServer> serverMap = new ConcurrentHashMap<String, ExchangeServer>(); // <host:port,Exchanger>
private final Map<String, ReferenceCountExchangeClient> referenceClientMap = new ConcurrentHashMap<String, ReferenceCountExchangeClient>(); // <host:port,Exchanger>
private final ConcurrentMap<String, LazyConnectExchangeClient> ghostClientMap = new ConcurrentHashMap<String, LazyConnectExchangeClient>();
private final ConcurrentMap<String, Object> locks = new ConcurrentHashMap<String, Object>();
private final Set<String> optimizers = new ConcurrentHashSet<String>();
//consumer side export a stub service for dispatching event
//servicekey-stubmethods
Expand Down Expand Up @@ -373,11 +374,18 @@ private ExchangeClient getSharedClient(URL url) {
referenceClientMap.remove(key);
}
}
synchronized (key.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;
}
}
Expand Down

0 comments on commit 1f63dc4

Please sign in to comment.