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 Sep 12, 2016
1 parent a535b96 commit 7a38384
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 @@ -20,6 +20,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 org.infinispan.Cache;
import org.infinispan.IllegalLifecycleStateException;
Expand Down Expand Up @@ -135,7 +137,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 @@ -603,6 +606,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 @@ -638,6 +645,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 @@ -687,14 +695,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 7a38384

Please sign in to comment.