Skip to content

Apache Lucene open-source search software

Fix circular dependency deadlock in TestSecrets initialization (#15864)

Namgyu Kim83e3f9ac24ac
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index ea86c442197..e18ae082486 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -209,7 +209,6 @@ Other
* GITHUB#15844: Detect JVM bitness using Panama's ValueLayout#ADDRESS instead
of undocumented system properties. (Uwe Schindler)
-
======================= Lucene 10.5.0 =======================
API Changes
@@ -283,6 +282,9 @@ Bug Fixes
native code at all (wrong platform); disable native code on 32 bit platforms.
(Uwe Schindler, thanks to Christian Ortlepp for finding the issue)
+* GITHUB#12419, GITHUB#15119, GITHUB#15864: Fix circular dependency deadlock in
+ TestSecrets initialization (Namgyu Kim, Uwe Schindler)
+
Other
---------------------
* GITHUB#15586: Document that scoring and ranking may change across major Lucene versions, and that applications requiring stable ranking should explicitly configure Similarity. (Parveen Saini)
diff --git a/lucene/core/src/java/org/apache/lucene/internal/tests/TestSecrets.java b/lucene/core/src/java/org/apache/lucene/internal/tests/TestSecrets.java
index 339e9fa3377..59a6221bbea 100644
--- a/lucene/core/src/java/org/apache/lucene/internal/tests/TestSecrets.java
+++ b/lucene/core/src/java/org/apache/lucene/internal/tests/TestSecrets.java
@@ -17,8 +17,8 @@
package org.apache.lucene.internal.tests;
import java.lang.StackWalker.StackFrame;
+import java.lang.invoke.MethodHandles;
import java.util.Objects;
-import java.util.function.Consumer;
import org.apache.lucene.index.ConcurrentMergeScheduler;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
@@ -31,25 +31,15 @@ import org.apache.lucene.store.FilterIndexInput;
* initialized once on startup.
*/
public final class TestSecrets {
- static {
- Consumer<Class<?>> ensureInitialized =
- clazz -> {
- try {
- // A no-op forName here has a side-effect of ensuring the class is loaded and
- // initialized.
- // This only happens once. We could just leverage the JLS and invoke a static
- // method (or a constructor) on the target class but the method below seems simpler.
- // TODO: In Java 15 there's MethodHandles.lookup().ensureInitialized(clazz)
- Class.forName(clazz.getName());
- } catch (ClassNotFoundException e) {
- throw new RuntimeException(e);
- }
- };
-
- ensureInitialized.accept(ConcurrentMergeScheduler.class);
- ensureInitialized.accept(SegmentReader.class);
- ensureInitialized.accept(IndexWriter.class);
- ensureInitialized.accept(FilterIndexInput.class);
+
+ private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
+
+ private static void ensureInitialized(Class<?> clazz) {
+ try {
+ LOOKUP.ensureInitialized(clazz);
+ } catch (IllegalAccessException e) {
+ throw new AssertionError(e);
+ }
}
@SuppressWarnings("NonFinalStaticField")
@@ -71,72 +61,107 @@ public final class TestSecrets {
/** Return the accessor to internal secrets for an {@link IndexReader}. */
public static IndexPackageAccess getIndexPackageAccess() {
- ensureCaller();
+ ensureCallerForGetter();
+ if (indexWriterAccess == null) {
+ ensureInitialized(IndexWriter.class);
+ }
return Objects.requireNonNull(indexPackageAccess);
}
/** Return the accessor to internal secrets for an {@link ConcurrentMergeScheduler}. */
public static ConcurrentMergeSchedulerAccess getConcurrentMergeSchedulerAccess() {
- ensureCaller();
+ ensureCallerForGetter();
+ if (cmsAccess == null) {
+ ensureInitialized(ConcurrentMergeScheduler.class);
+ }
return Objects.requireNonNull(cmsAccess);
}
/** Return the accessor to internal secrets for an {@link SegmentReader}. */
public static SegmentReaderAccess getSegmentReaderAccess() {
- ensureCaller();
+ ensureCallerForGetter();
+ if (segmentReaderAccess == null) {
+ ensureInitialized(SegmentReader.class);
+ }
return Objects.requireNonNull(segmentReaderAccess);
}
/** Return the accessor to internal secrets for an {@link IndexWriter}. */
public static IndexWriterAccess getIndexWriterAccess() {
- ensureCaller();
+ ensureCallerForGetter();
+ if (indexWriterAccess == null) {
+ ensureInitialized(IndexWriter.class);
+ }
return Objects.requireNonNull(indexWriterAccess);
}
/** Return the accessor to internal secrets for an {@link FilterIndexInput}. */
public static FilterIndexInputAccess getFilterInputIndexAccess() {
- ensureCaller();
+ ensureCallerForGetter();
+ if (filterIndexInputAccess == null) {
+ ensureInitialized(FilterIndexInput.class);
+ }
return Objects.requireNonNull(filterIndexInputAccess);
}
/** For internal initialization only. */
public static void setIndexWriterAccess(IndexWriterAccess indexWriterAccess) {
+ ensureCallerForSetter(IndexWriter.class);
ensureNull(TestSecrets.indexWriterAccess);
TestSecrets.indexWriterAccess = indexWriterAccess;
}
/** For internal initialization only. */
public static void setIndexPackageAccess(IndexPackageAccess indexPackageAccess) {
+ ensureCallerForSetter(IndexWriter.class);
ensureNull(TestSecrets.indexPackageAccess);
TestSecrets.indexPackageAccess = indexPackageAccess;
}
/** For internal initialization only. */
public static void setConcurrentMergeSchedulerAccess(ConcurrentMergeSchedulerAccess cmsAccess) {
+ ensureCallerForSetter(ConcurrentMergeScheduler.class);
ensureNull(TestSecrets.cmsAccess);
TestSecrets.cmsAccess = cmsAccess;
}
/** For internal initialization only. */
public static void setSegmentReaderAccess(SegmentReaderAccess segmentReaderAccess) {
+ ensureCallerForSetter(SegmentReader.class);
ensureNull(TestSecrets.segmentReaderAccess);
TestSecrets.segmentReaderAccess = segmentReaderAccess;
}
/** For internal initialization only. */
public static void setFilterInputIndexAccess(FilterIndexInputAccess filterIndexInputAccess) {
+ ensureCallerForSetter(FilterIndexInput.class);
ensureNull(TestSecrets.filterIndexInputAccess);
TestSecrets.filterIndexInputAccess = filterIndexInputAccess;
}
private static void ensureNull(Object ob) {
if (ob != null) {
- throw new AssertionError(
+ throw new UnsupportedOperationException(
"The accessor is already set. It can only be called from inside Lucene Core.");
}
}
- private static void ensureCaller() {
+ private static void ensureCallerForSetter(Class<?> allowedCaller) {
+ final boolean validCaller =
+ StackWalker.getInstance()
+ .walk(
+ s ->
+ s.skip(2)
+ .limit(1)
+ .map(StackFrame::getClassName)
+ .allMatch(c -> Objects.equals(c, allowedCaller.getName())));
+ if (!validCaller) {
+ throw new UnsupportedOperationException(
+ "The accessor can only be set by " + allowedCaller.getName() + ".");
+ }
+ }
+
+ private static void ensureCallerForGetter() {
final boolean validCaller =
StackWalker.getInstance()
.walk(
@@ -144,7 +169,11 @@ public final class TestSecrets {
s.skip(2)
.limit(1)
.map(StackFrame::getClassName)
- .allMatch(c -> c.startsWith("org.apache.lucene.tests.")));
+ .allMatch(
+ c ->
+ c.startsWith("org.apache.lucene.tests.")
+ || c.equals(
+ "org.apache.lucene.index.TestClassloadingDeadlock")));
if (!validCaller) {
throw new UnsupportedOperationException(
"Lucene TestSecrets can only be used by the test-framework.");
diff --git a/lucene/core/src/test/org/apache/lucene/codecs/TestCodecLoadingDeadlock.java b/lucene/core/src/test/org/apache/lucene/index/TestClassloadingDeadlock.java
similarity index 81%
rename from lucene/core/src/test/org/apache/lucene/codecs/TestCodecLoadingDeadlock.java
rename to lucene/core/src/test/org/apache/lucene/index/TestClassloadingDeadlock.java
index 0e0c3941bec..24c52c15d46 100644
--- a/lucene/core/src/test/org/apache/lucene/codecs/TestCodecLoadingDeadlock.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestClassloadingDeadlock.java
@@ -14,12 +14,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.lucene.codecs;
+package org.apache.lucene.index;
import com.carrotsearch.randomizedtesting.LifecycleScope;
import com.carrotsearch.randomizedtesting.RandomizedContext;
import com.carrotsearch.randomizedtesting.RandomizedRunner;
import com.carrotsearch.randomizedtesting.RandomizedTest;
+import java.lang.invoke.MethodHandles;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -29,6 +30,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.util.Set;
+import java.util.concurrent.Callable;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
@@ -36,6 +38,10 @@ import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
+import org.apache.lucene.codecs.Codec;
+import org.apache.lucene.codecs.DocValuesFormat;
+import org.apache.lucene.codecs.PostingsFormat;
+import org.apache.lucene.internal.tests.TestSecrets;
import org.apache.lucene.tests.util.LuceneTestCase;
import org.apache.lucene.util.NamedThreadFactory;
import org.apache.lucene.util.SuppressForbidden;
@@ -47,7 +53,7 @@ import org.junit.runner.RunWith;
* initialization when spawned as subprocess (and please let default codecs alive)! */
@RunWith(RandomizedRunner.class)
-public class TestCodecLoadingDeadlock extends Assert {
+public class TestClassloadingDeadlock extends Assert {
private static final int MAX_TIME_SECONDS = 30;
@Test
@@ -99,7 +105,21 @@ public class TestCodecLoadingDeadlock extends Assert {
final String pfName = args[1];
final String dvfName = args[2];
- final int numThreads = 14; // two times the modulo in switch statement below
+ final var lookup = MethodHandles.lookup();
+ final List<Callable<?>> tasks =
+ List.of(
+ Codec::getDefault,
+ () -> Codec.forName(codecName),
+ () -> PostingsFormat.forName(pfName),
+ () -> DocValuesFormat.forName(dvfName),
+ Codec::availableCodecs,
+ PostingsFormat::availablePostingsFormats,
+ DocValuesFormat::availableDocValuesFormats,
+ TestSecrets::getIndexWriterAccess,
+ () -> lookup.ensureInitialized(IndexWriter.class),
+ () -> lookup.ensureInitialized(SegmentReader.class));
+
+ final int numTasks = tasks.size(), numThreads = numTasks * 2;
final CopyOnWriteArrayList<Thread> allThreads = new CopyOnWriteArrayList<>();
final ExecutorService pool =
Executors.newFixedThreadPool(
@@ -123,36 +143,11 @@ public class TestCodecLoadingDeadlock extends Assert {
// Await a common barrier point for all threads and then
// run racy code. This is intentional.
barrier.await();
- switch (taskNo % 7) {
- case 0:
- Codec.getDefault();
- break;
- case 1:
- Codec.forName(codecName);
- break;
- case 2:
- PostingsFormat.forName(pfName);
- break;
- case 3:
- DocValuesFormat.forName(dvfName);
- break;
- case 4:
- Codec.availableCodecs();
- break;
- case 5:
- PostingsFormat.availablePostingsFormats();
- break;
- case 6:
- DocValuesFormat.availableDocValuesFormats();
- break;
- default:
- throw new AssertionError();
- }
+ tasks.get(taskNo % numTasks).call();
} catch (Throwable t) {
synchronized (args) {
System.err.println(
- Thread.currentThread().getName()
- + " failed to lookup codec service:");
+ Thread.currentThread().getName() + " failed to call racy method:");
t.printStackTrace(System.err);
}
Runtime.getRuntime().halt(1); // signal failure to caller
diff --git a/lucene/core/src/test/org/apache/lucene/internal/tests/TestTestSecrets.java b/lucene/core/src/test/org/apache/lucene/internal/tests/TestTestSecrets.java
index 6da41fb3c8b..a3655444a89 100644
--- a/lucene/core/src/test/org/apache/lucene/internal/tests/TestTestSecrets.java
+++ b/lucene/core/src/test/org/apache/lucene/internal/tests/TestTestSecrets.java
@@ -32,9 +32,13 @@ public class TestTestSecrets extends LuceneTestCase {
}
public void testCannotSet() {
- expectThrows(AssertionError.class, () -> TestSecrets.setIndexWriterAccess(null));
- expectThrows(AssertionError.class, () -> TestSecrets.setConcurrentMergeSchedulerAccess(null));
- expectThrows(AssertionError.class, () -> TestSecrets.setIndexPackageAccess(null));
- expectThrows(AssertionError.class, () -> TestSecrets.setSegmentReaderAccess(null));
+ expectThrows(UnsupportedOperationException.class, () -> TestSecrets.setIndexWriterAccess(null));
+ expectThrows(
+ UnsupportedOperationException.class,
+ () -> TestSecrets.setConcurrentMergeSchedulerAccess(null));
+ expectThrows(
+ UnsupportedOperationException.class, () -> TestSecrets.setIndexPackageAccess(null));
+ expectThrows(
+ UnsupportedOperationException.class, () -> TestSecrets.setSegmentReaderAccess(null));
}
}