You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ch...@apache.org on 2025/10/10 07:23:36 UTC

(lucene) branch branch_10x updated: Avoid static dependency from TermsEnum to one of its subclasses (#15319)

This is an automated email from the ASF dual-hosted git repository.

chegar pushed a commit to branch branch_10x
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/branch_10x by this push:
     new 9393df627ea Avoid static dependency from TermsEnum to one of its subclasses (#15319)
9393df627ea is described below

commit 9393df627ea30dc72bbe1e24bf63c761430a4070
Author: Chris Hegarty <62...@users.noreply.github.com>
AuthorDate: Fri Oct 10 08:20:47 2025 +0100

    Avoid static dependency from TermsEnum to one of its subclasses (#15319)
    
    Fixes a potential hang with initialisation of TermsEnum and BaseTermsEnum, by simply removing the dependency and replicating the small amount of code.
    
    Problem description: the static TermsEnum.EMPTY initialises to an implementation of BaseTermsEnum. TermsEnum is a superclass of BaseTermsEnum, so there is a clear dependency between these classes. If a subclass of BaseTermsEnum is initialising it may grab the lock on BaseTermsEnum, and prevent TermsEnum from initialising.
    
    ---------
    
    Co-authored-by: Robert Muir <rm...@apache.org>
---
 lucene/CHANGES.txt                                 |  2 ++
 .../java/org/apache/lucene/index/TermsEnum.java    | 24 +++++++++++++++++++++-
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 2b43cb73a9e..443e84af0f8 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -70,6 +70,8 @@ Bug Fixes
 
 * GITHUB#15213: Don't normalize centroids with zero samples, which caused divide-by-zero. (Mike Sokolov)
 
+* 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 2ff6a2719b6..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,8 +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.
    */
+  // 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) {