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

reta pushed a commit to branch 3.6.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit ffea0e817270b0862b22d0d0779bf50ff513fcfd
Author: Guillaume Nodet <[email protected]>
AuthorDate: Wed Mar 11 21:41:58 2026 +0100

    Fix test cleanup in Undertow and race condition in JMS tests (#2947)
    
    Undertow: Add @After cleanup to properly destroy server engines
    between tests, preventing cascading port-bind failures when a
    test creates an engine but doesn't clean up.
    
    JMS: Fix TOCTOU race condition in waitForReceiveInMessage() and
    waitForReceiveDestMessage() where the null check was outside the
    synchronized block. If a message arrived between the check and the
    wait(), the notification was lost, causing the test to wait the
    full timeout before failing.
    
    Co-authored-by: Claude Opus 4.6 <[email protected]>
    (cherry picked from commit a67861d2133895de2aa26a8e12b7559115a437ce)
---
 .../http_undertow/UndertowHTTPServerEngineTest.java   | 19 +++++++------------
 .../apache/cxf/transport/jms/AbstractJMSTester.java   | 14 +++++++-------
 2 files changed, 14 insertions(+), 19 deletions(-)

diff --git 
a/rt/transports/http-undertow/src/test/java/org/apache/cxf/transport/http_undertow/UndertowHTTPServerEngineTest.java
 
b/rt/transports/http-undertow/src/test/java/org/apache/cxf/transport/http_undertow/UndertowHTTPServerEngineTest.java
index 9daa8f2bd7..0b0c07b8e3 100644
--- 
a/rt/transports/http-undertow/src/test/java/org/apache/cxf/transport/http_undertow/UndertowHTTPServerEngineTest.java
+++ 
b/rt/transports/http-undertow/src/test/java/org/apache/cxf/transport/http_undertow/UndertowHTTPServerEngineTest.java
@@ -42,6 +42,7 @@ import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.management.InstrumentationManager;
 import org.apache.cxf.testutil.common.TestUtil;
 
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -77,7 +78,13 @@ public class UndertowHTTPServerEngineTest {
 
         factory = new UndertowHTTPServerEngineFactory();
         factory.setBus(bus);
+    }
 
+    @After
+    public void tearDown() throws Exception {
+        UndertowHTTPServerEngineFactory.destroyForPort(PORT1);
+        UndertowHTTPServerEngineFactory.destroyForPort(PORT2);
+        UndertowHTTPServerEngineFactory.destroyForPort(PORT3);
     }
 
 
@@ -90,8 +97,6 @@ public class UndertowHTTPServerEngineTest {
         assertTrue(
             "Engine references for the same port should point to the same 
instance",
             engine == factory.retrieveUndertowHTTPServerEngine(PORT1));
-
-        UndertowHTTPServerEngineFactory.destroyForPort(PORT1);
     }
 
     @Test
@@ -129,10 +134,6 @@ public class UndertowHTTPServerEngineTest {
         engine = factory.createUndertowHTTPServerEngine(PORT3, "https");
         assertTrue("Protocol must be https",
                    "https".equals(engine.getProtocol()));
-
-        UndertowHTTPServerEngineFactory.destroyForPort(PORT1);
-        UndertowHTTPServerEngineFactory.destroyForPort(PORT2);
-        UndertowHTTPServerEngineFactory.destroyForPort(PORT3);
     }
 
     @Test
@@ -186,9 +187,6 @@ public class UndertowHTTPServerEngineTest {
         engine.shutdown();
         response = getResponse(urlStr2);
         assertEquals("The undertow http handler did not take effect", 
response, "string2");
-        // set the get request
-        UndertowHTTPServerEngineFactory.destroyForPort(PORT1);
-
     }
 
     /**
@@ -233,9 +231,6 @@ public class UndertowHTTPServerEngineTest {
         s = CastUtils.cast(ManagementFactory.getPlatformMBeanServer().
             queryNames(new 
ObjectName("org.xnio:type=Xnio,provider=\"nio\",worker=\"*\""), null));
         assertEquals("Could not find 0 Undertow Server: " + s, 0, s.size());
-
-        UndertowHTTPServerEngineFactory.destroyForPort(PORT1);
-        UndertowHTTPServerEngineFactory.destroyForPort(PORT2);
     }
 
     private static String getResponse(String target) throws Exception {
diff --git 
a/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/AbstractJMSTester.java
 
b/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/AbstractJMSTester.java
index a722a2dd6c..463fc62fcd 100644
--- 
a/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/AbstractJMSTester.java
+++ 
b/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/AbstractJMSTester.java
@@ -260,23 +260,23 @@ public abstract class AbstractJMSTester {
 
 
     protected Message waitForReceiveInMessage() throws InterruptedException {
-        if (null == inMessage.get()) {
-            synchronized (inMessage) {
+        synchronized (inMessage) {
+            if (null == inMessage.get()) {
                 inMessage.wait(MAX_RECEIVE_TIME * 1000L);
             }
-            assertNotNull("Can't receive the Conduit Message in " + 
MAX_RECEIVE_TIME + " seconds", inMessage.get());
         }
+        assertNotNull("Can't receive the Conduit Message in " + 
MAX_RECEIVE_TIME + " seconds", inMessage.get());
         return inMessage.getAndSet(null);
     }
 
     protected Message waitForReceiveDestMessage() throws InterruptedException {
-        if (null == destMessage.get()) {
-            synchronized (destMessage) {
+        synchronized (destMessage) {
+            if (null == destMessage.get()) {
                 destMessage.wait(MAX_RECEIVE_TIME * 1000L);
             }
-            assertNotNull("Can't receive the Destination message in " + 
MAX_RECEIVE_TIME + " seconds",
-                    destMessage.get());
         }
+        assertNotNull("Can't receive the Destination message in " + 
MAX_RECEIVE_TIME + " seconds",
+            destMessage.get());
         return destMessage.getAndSet(null);
     }
 

Reply via email to