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

daim pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 6536145b17 OAK-12074 : added awaitUninterruptibly() in oak-commons 
(#2703)
6536145b17 is described below

commit 6536145b1750f3e1babef646da3b4a08ab351d26
Author: Rishabh Kumar <[email protected]>
AuthorDate: Tue Jan 27 19:15:54 2026 +0530

    OAK-12074 : added awaitUninterruptibly() in oak-commons (#2703)
    
    * OAK-12074 : added awaitUninterruptibly() in oak-commons
    
    * OAK-12074 : removed overloaded method, since it is not used
---
 .../internal/concurrent/UninterruptibleUtils.java  | 67 +++++++++++++++++++
 .../concurrent/UninterruptibleUtilsTest.java       | 77 ++++++++++++++++++++++
 2 files changed, 144 insertions(+)

diff --git 
a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/internal/concurrent/UninterruptibleUtils.java
 
b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/internal/concurrent/UninterruptibleUtils.java
new file mode 100644
index 0000000000..dfd908469e
--- /dev/null
+++ 
b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/internal/concurrent/UninterruptibleUtils.java
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.jackrabbit.oak.commons.internal.concurrent;
+
+import java.util.Objects;
+import java.util.concurrent.CountDownLatch;
+
+/**
+ * Utility methods for waiting on synchronization primitives without
+ * propagating {@link InterruptedException} to callers.
+ */
+public class UninterruptibleUtils {
+
+    private UninterruptibleUtils() {
+        // no instance for you
+    }
+
+    /**
+     * Waits uninterruptibly until the given {@link CountDownLatch} reaches 
zero.
+     * <p>
+     * This method repeatedly invokes {@link CountDownLatch#await()} and
+     * ignores any {@link InterruptedException} that occurs while waiting,
+     * but remembers that an interruption happened. After the latch has
+     * reached zero (or the method otherwise returns), this method restores
+     * the thread's interrupted status if any interruptions were detected
+     * during the wait.
+     *
+     * @param latch the latch to wait on; must not be {@code null}
+     * @throws NullPointerException if {@code latch} is {@code null}
+     */
+    public static void awaitUninterruptibly(CountDownLatch latch) {
+
+        Objects.requireNonNull(latch, "latch is null");
+
+        boolean interrupted = false;
+        try {
+            for (;;) {
+                try {
+                    latch.await();
+                    return;           // completed normally
+                } catch (InterruptedException e) {
+                    interrupted = true; // remember and retry
+                }
+            }
+        } finally {
+            if (interrupted) {
+                Thread.currentThread().interrupt(); // restore flag
+            }
+        }
+    }
+}
diff --git 
a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/internal/concurrent/UninterruptibleUtilsTest.java
 
b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/internal/concurrent/UninterruptibleUtilsTest.java
new file mode 100644
index 0000000000..45f945c9a1
--- /dev/null
+++ 
b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/internal/concurrent/UninterruptibleUtilsTest.java
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.jackrabbit.oak.commons.internal.concurrent;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.concurrent.CountDownLatch;
+
+/**
+ * Unit cases for {@link UninterruptibleUtils}
+ */
+public class UninterruptibleUtilsTest {
+
+    @Test
+    public void testNullLatch() {
+        Assert.assertThrows(NullPointerException.class,
+                () -> UninterruptibleUtils.awaitUninterruptibly(null));
+    }
+
+    @Test
+    public void testWaitsUntilLatchReachesZero() throws Exception {
+        CountDownLatch latch = new CountDownLatch(1);
+
+        Thread t = new Thread(() -> 
UninterruptibleUtils.awaitUninterruptibly(latch));
+        t.start();
+
+        // Ensure the thread is actually waiting
+        Thread.sleep(5);
+        Assert.assertTrue(t.isAlive());
+
+        latch.countDown();
+        t.join(10);
+
+        Assert.assertFalse(t.isAlive());
+    }
+
+    @Test
+    public void testSwallowInterruptsButRestoreFlag() throws Exception {
+        CountDownLatch latch = new CountDownLatch(1);
+
+        Thread t = new Thread(() -> {
+            UninterruptibleUtils.awaitUninterruptibly(latch);
+            // After returning, interrupted flag should be set if we 
interrupted during wait
+            Assert.assertTrue(Thread.currentThread().isInterrupted());
+        });
+
+        t.start();
+        Thread.sleep(5);
+
+        // Interrupt while it's waiting
+        t.interrupt();
+
+        Thread.sleep(5);
+        latch.countDown();
+        t.join(10);
+
+        Assert.assertFalse(t.isAlive());
+    }
+
+}
\ No newline at end of file

Reply via email to