Skip to content

Commit

Permalink
ISPN-6869 Avoid deadlock when stopping DefaultCacheManager
Browse files Browse the repository at this point in the history
  • Loading branch information
vjuranek committed Jul 19, 2016
1 parent b7241fc commit 5910903
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions core/src/main/java/org/infinispan/manager/DefaultCacheManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import static org.infinispan.factories.KnownComponentNames.CACHE_DEPENDENCY_GRAPH;

Expand Down Expand Up @@ -134,7 +136,8 @@ public class DefaultCacheManager implements EmbeddedCacheManager {
private final CacheContainerStats stats;
private final ConfigurationManager configurationManager;

@GuardedBy("this")
private final ReadWriteLock stoppingLock = new ReentrantReadWriteLock();
@GuardedBy("stoppingLock")
private boolean stopping;

/**
Expand Down Expand Up @@ -602,6 +605,10 @@ private <K, V> Cache<K, V> wireAndStartCache(String cacheName, String configurat
Configuration c;
boolean needToNotifyCacheStarted = false;
try {
stoppingLock.readLock().lock();
if (stopping) {
throw new IllegalStateException("cache manager is being stopped, cannot create new cache now");
}
synchronized (caches) {
//fetch it again with the lock held
CacheWrapper existingCacheWrapper = caches.get(cacheName);
Expand Down Expand Up @@ -637,6 +644,7 @@ private <K, V> Cache<K, V> wireAndStartCache(String cacheName, String configurat
needToNotifyCacheStarted = notStartedYet && cr.getStatus() == ComponentStatus.RUNNING;
return cache;
} finally {
stoppingLock.readLock().unlock();
// allow other threads to access the cache
if (createdCacheWrapper != null) {
log.tracef("Closing latch for cache %s", cacheName);
Expand Down Expand Up @@ -686,14 +694,20 @@ private void terminate(String cacheName) {
public void stop() {
authzHelper.checkPermission(AuthorizationPermission.LIFECYCLE);

synchronized (this) {
stoppingLock.writeLock().lock();
try {
if (stopping) {
log.trace("Ignore call to stop as the cache manager is stopping");
return;
}

log.debugf("Stopping cache manager %s on %s", configurationManager.getGlobalConfiguration().transport().clusterName(), getAddress());
stopping = true;
} finally {
stoppingLock.writeLock().unlock();
}

synchronized (this) {
stopCaches();
globalComponentRegistry.getComponent(CacheManagerJmxRegistration.class).stop();
globalComponentRegistry.stop();
Expand Down

0 comments on commit 5910903

Please sign in to comment.