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

ijuma pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/kafka.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 77215eded7b KAFKA-14792: Race condition in LazyIndex.get() (#13359)
77215eded7b is described below

commit 77215eded7b23aa06f3a4919233df175d5aa3359
Author: Ismael Juma <ism...@juma.me.uk>
AuthorDate: Tue Mar 7 15:56:24 2023 -0800

    KAFKA-14792: Race condition in LazyIndex.get() (#13359)
    
    `LazyIndex.get()` has a race condition that can result in a 
ClassCastException being thrown in some cases.
    
    This was introduced when `LazyIndex` was rewritten from Scala to Java.
    
    I didn't include a test since it's a bit overkill to add a concurrent test 
for this.
    
    Reviewers: Jun Rao <jun...@gmail.com>
---
 .../kafka/storage/internals/log/LazyIndex.java     | 23 +++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

diff --git 
a/storage/src/main/java/org/apache/kafka/storage/internals/log/LazyIndex.java 
b/storage/src/main/java/org/apache/kafka/storage/internals/log/LazyIndex.java
index 1172bb596e7..9d0725e0e33 100644
--- 
a/storage/src/main/java/org/apache/kafka/storage/internals/log/LazyIndex.java
+++ 
b/storage/src/main/java/org/apache/kafka/storage/internals/log/LazyIndex.java
@@ -166,20 +166,25 @@ public class LazyIndex<T extends AbstractIndex> {
 
     @SuppressWarnings("unchecked")
     public T get() throws IOException {
-        if (indexWrapper instanceof IndexValue<?>)
-            return ((IndexValue<T>) indexWrapper).index;
-        else if (indexWrapper instanceof IndexFile) {
+        IndexWrapper wrapper = indexWrapper;
+        if (wrapper instanceof IndexValue<?>)
+            return ((IndexValue<T>) wrapper).index;
+        else {
             lock.lock();
             try {
-                IndexFile indexFile = (IndexFile) indexWrapper;
-                IndexValue<T> indexValue = new 
IndexValue<>(loadIndex(indexFile.file));
-                indexWrapper = indexValue;
-                return indexValue.index;
+                if (indexWrapper instanceof IndexValue<?>)
+                    return ((IndexValue<T>) indexWrapper).index;
+                else if (indexWrapper instanceof IndexFile) {
+                    IndexFile indexFile = (IndexFile) indexWrapper;
+                    IndexValue<T> indexValue = new 
IndexValue<>(loadIndex(indexFile.file));
+                    indexWrapper = indexValue;
+                    return indexValue.index;
+                } else
+                    throw new IllegalStateException("Unexpected type for 
indexWrapper " + indexWrapper.getClass());
             } finally {
                 lock.unlock();
             }
-        } else
-            throw new IllegalStateException("Unexpected type for indexWrapper 
" + indexWrapper.getClass());
+        }
     }
 
     public void updateParentDir(File parentDir) {

Reply via email to