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

olamy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git


The following commit(s) were added to refs/heads/master by this push:
     new 155e93af4 Fix spurious wakeup in CountdownCloseable.awaitClosed() 
(#3603)
155e93af4 is described below

commit 155e93af4c02723e6c44b47a759cba87a1c78068
Author: Arturo Bernal <[email protected]>
AuthorDate: Mon Nov 23 12:14:52 2026 +0100

    Fix spurious wakeup in CountdownCloseable.awaitClosed() (#3603)
    
    wait() was called inside an if instead of a while, so a spurious
    wakeup (permitted by the JLS) could let awaitClosed() return before
    the countdown actually reached zero and close() notified. This is
    used by CommandlineExecutor.awaitExit() and ForkStarter to wait for
    stream-reader threads to finish draining a forked process's output,
    so an early return can race with output still in flight.
    
    Fixes #3366.
    
    Claude-Session: https://claude.ai/code/session_01BrsHTdDq7PD1DoqeA9vs66
---
 .../extensions/util/CountdownCloseable.java        |  4 +-
 .../extensions/util/CountdownCloseableTest.java    | 75 ++++++++++++++++++++++
 2 files changed, 78 insertions(+), 1 deletion(-)

diff --git 
a/surefire-extensions-api/src/main/java/org/apache/maven/surefire/extensions/util/CountdownCloseable.java
 
b/surefire-extensions-api/src/main/java/org/apache/maven/surefire/extensions/util/CountdownCloseable.java
index 02f7846df..cff833b35 100644
--- 
a/surefire-extensions-api/src/main/java/org/apache/maven/surefire/extensions/util/CountdownCloseable.java
+++ 
b/surefire-extensions-api/src/main/java/org/apache/maven/surefire/extensions/util/CountdownCloseable.java
@@ -53,7 +53,9 @@ public synchronized void close() throws IOException {
      * @throws InterruptedException see {@link Object#wait()}
      */
     public synchronized void awaitClosed() throws InterruptedException {
-        if (countdown > 0) {
+        // wait() must be called in a loop: a spurious wakeup (JLS 17.2) can 
return control
+        // before close() has actually decremented the countdown to zero and 
notified.
+        while (countdown > 0) {
             wait();
         }
     }
diff --git 
a/surefire-extensions-api/src/test/java/org/apache/maven/surefire/extensions/util/CountdownCloseableTest.java
 
b/surefire-extensions-api/src/test/java/org/apache/maven/surefire/extensions/util/CountdownCloseableTest.java
new file mode 100644
index 000000000..851571532
--- /dev/null
+++ 
b/surefire-extensions-api/src/test/java/org/apache/maven/surefire/extensions/util/CountdownCloseableTest.java
@@ -0,0 +1,75 @@
+/*
+ * 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.maven.surefire.extensions.util;
+
+import java.io.Closeable;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+
+/**
+ *
+ */
+class CountdownCloseableTest {
+
+    @Test
+    void awaitClosedShouldIgnoreSpuriousWakeup() throws Exception {
+        CountdownCloseable countdownCloseable = new 
CountdownCloseable(mock(Closeable.class), 1);
+        AtomicBoolean returnedFromAwait = new AtomicBoolean(false);
+
+        Thread waiter = new Thread(() -> {
+            try {
+                countdownCloseable.awaitClosed();
+                returnedFromAwait.set(true);
+            } catch (InterruptedException e) {
+                Thread.currentThread().interrupt();
+            }
+        });
+        waiter.start();
+        awaitState(waiter, Thread.State.WAITING);
+
+        // Simulate a spurious wakeup (JLS 17.2): notifying the monitor 
without the
+        // countdown having reached zero must not let awaitClosed() return.
+        synchronized (countdownCloseable) {
+            countdownCloseable.notifyAll();
+        }
+        waiter.join(300L);
+        assertThat(waiter.isAlive()).isTrue();
+        assertThat(returnedFromAwait).isFalse();
+
+        countdownCloseable.close();
+        waiter.join(5_000L);
+        assertThat(waiter.isAlive()).isFalse();
+        assertThat(returnedFromAwait).isTrue();
+    }
+
+    private static void awaitState(Thread thread, Thread.State state) throws 
InterruptedException {
+        long deadline = System.currentTimeMillis() + 5_000L;
+        while (thread.getState() != state) {
+            if (System.currentTimeMillis() > deadline) {
+                throw new AssertionError(
+                        "Thread " + thread.getName() + " never reached state " 
+ state + ", was " + thread.getState());
+            }
+            Thread.sleep(10L);
+        }
+    }
+}

Reply via email to