Skip to content

Apache Lucene open-source search software

Avoid static dependency from TermsEnum to one of its subclasses (#15319)

Chris Hegartyaa4f14313caf
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index f48b97455d9..ab82a193c5c 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -199,6 +199,8 @@ Bug Fixes
removing co-planar points to do it once after all holes are removed instead of doing it for each
removed hole.
+* GITHUB#15319: Fix potential hang in initialisation of TermsEnum and BaseTermsEnum (Chris Hegarty)
+
Other
---------------------
* GITHUB#15237: Fix SmartChinese to only deserialize dictionary data from classpath with a native array
diff --git a/lucene/core/src/java/org/apache/lucene/index/TermsEnum.java b/lucene/core/src/java/org/apache/lucene/index/TermsEnum.java
index 8e2a2e89db8..6bf12e7a037 100644
--- a/lucene/core/src/java/org/apache/lucene/index/TermsEnum.java
+++ b/lucene/core/src/java/org/apache/lucene/index/TermsEnum.java
@@ -196,9 +196,30 @@ public abstract class TermsEnum implements BytesRefIterator {
* Attributes to it. This should not be a problem, as the enum is always empty and the existence
* of unused Attributes does not matter.
*/
- @SuppressWarnings("ClassInitializationDeadlock") // FIXME: may cause hangs!
+ // Avoid refactoring that results in a dependency on a subclass, like BaseTermsEnum.
+ // See: https://github.com/apache/lucene/issues/15317
public static final TermsEnum EMPTY =
- new BaseTermsEnum() {
+ new TermsEnum() {
+
+ private AttributeSource atts;
+
+ @Override
+ public AttributeSource attributes() {
+ if (atts == null) {
+ atts = new AttributeSource();
+ }
+ return atts;
+ }
+
+ @Override
+ public boolean seekExact(BytesRef text) {
+ return seekCeil(text) == SeekStatus.FOUND;
+ }
+
+ @Override
+ public IOBooleanSupplier prepareSeekExact(BytesRef text) {
+ return () -> seekExact(text);
+ }
@Override
public SeekStatus seekCeil(BytesRef term) {