Apache Solr open-source search software
SOLR-4595: CachingDirectoryFactory#close can throw a concurrent modification exception.
Mark Robert Millerc216ff7d8292
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 83a3b32dc2a..78ce085a901 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -137,6 +137,9 @@ Bug Fixes* SOLR-4573: Accessing Admin UI files in SolrCloud mode logs warnings.(Mark Miller, Phil John)+* SOLR-4595: CachingDirectoryFactory#close can throw a concurrent+ modification exception. (Mark Miller)+Optimizations----------------------diff --git a/solr/core/src/java/org/apache/solr/core/CachingDirectoryFactory.java b/solr/core/src/java/org/apache/solr/core/CachingDirectoryFactory.javaindex 45a9d01f009..33a53b67dbe 100644--- a/solr/core/src/java/org/apache/solr/core/CachingDirectoryFactory.java+++ b/solr/core/src/java/org/apache/solr/core/CachingDirectoryFactory.java@@ -20,6 +20,7 @@ package org.apache.solr.core;import java.io.File;import java.io.IOException;import java.util.ArrayList;+import java.util.Collection;import java.util.HashMap;import java.util.List;import java.util.Locale;@@ -123,7 +124,9 @@ public abstract class CachingDirectoryFactory extends DirectoryFactory {public void close() throws IOException {synchronized (this) {this.closed = true;- for (CacheValue val : byDirectoryCache.values()) {+ Collection<CacheValue> values = new ArrayList<CacheValue>();+ values.addAll(byDirectoryCache.values());+ for (CacheValue val : values) {try {// if there are still refs out, we have to wait for themint cnt = 0;@@ -135,7 +138,15 @@ public abstract class CachingDirectoryFactory extends DirectoryFactory {break;}}-+ assert val.refCnt == 0 : val.refCnt;+ } catch (Throwable t) {+ SolrException.log(log, "Error closing directory", t);+ }+ }++ values = byDirectoryCache.values();+ for (CacheValue val : values) {+ try {assert val.refCnt == 0 : val.refCnt;log.info("Closing directory when closing factory: " + val.path);closeDirectory(val);@@ -143,6 +154,7 @@ public abstract class CachingDirectoryFactory extends DirectoryFactory {SolrException.log(log, "Error closing directory", t);}}+byDirectoryCache.clear();byPathCache.clear();}diff --git a/solr/core/src/test/org/apache/solr/core/CachingDirectoryFactoryTest.java b/solr/core/src/test/org/apache/solr/core/CachingDirectoryFactoryTest.javaindex d09f103b93f..8d08d44e303 100644--- a/solr/core/src/test/org/apache/solr/core/CachingDirectoryFactoryTest.java+++ b/solr/core/src/test/org/apache/solr/core/CachingDirectoryFactoryTest.java@@ -73,6 +73,17 @@ public class CachingDirectoryFactoryTest extends SolrTestCaseJ4 {thread.join();}+ Thread thread = new Thread() {+ public void run() {+ try {+ df.close();+ } catch (IOException e) {+ throw new RuntimeException(e);+ }+ }+ };+ thread.start();+// do any remaining releasessynchronized (dirs) {int sz = dirs.size();@@ -98,7 +109,8 @@ public class CachingDirectoryFactoryTest extends SolrTestCaseJ4 {}- df.close();+ thread.join();+}private class ReleaseDirThread extends Thread {