[ 
https://issues.apache.org/jira/browse/CAMEL-27158?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18146832#comment-18146832
 ] 

Torsten Mielke commented on CAMEL-27158:
----------------------------------------

Investigated this in depth (re-reading the exact stack traces, checking the 
module's CI config, and comparing against prior art) — root cause looks like 
two distinct issues layered together:

*1. The rerun {{{}ConnectException{}}}/{{{}ClosedChannelException{}}} is a 
Spring {{ContextCache}} bug.*
{{WSAddressingTest}} is a plain {{@ExtendWith(SpringExtension.class)}} + bare 
{{@ContextConfiguration}} test with no {{{}@DirtiesContext{}}}. Ports are 
allocated in a static {{@BeforeAll}} and published as system properties that 
the Spring XML resolves via {{{}$\{WSAddressingTest.portN{}}}} placeholders 
when the {{ApplicationContext}} is built. Because nothing ever closes that 
context, Spring's {{ContextCache}} keeps serving the *same* context — including 
its still-bound {{routerEndpoint}} CXF/Undertow listener — across a Surefire 
rerun. The rerun's own {{@BeforeAll}} mints *new* ports (confirmed by the 
ticket itself: the rerun fails against a different address than attempt 1, only 
possible if {{@BeforeAll }}reran), but the cache key doesn't depend on those 
ports, so the rerun gets a cache hit on the stale context and the client ends 
up talking to an address nothing is listening on.

*2. The first-attempt {{BindException}} is most likely the same pre-existing, 
project-wide port race already fixed elsewhere — not specific to this test.*
{{AvailablePort.probePort()}} finds a free port via bind-read-close with zero 
cross-process coordination, a plain TOCTOU race against any other process on 
the host. Camel's CI ({{{}install-mvnd{}}} action) runs {{mvnd}} with 
{{{}-Dmvnd.threads=2{}}}, i.e. genuinely builds/tests two Maven modules 
concurrently on one shared runner. This exact {{BindException }}signature was 
already diagnosed and fixed for CAMEL-25788 / CAMEL-25583 / CAMEL-26365 (PR 
#28676), with a confirmed-via-reproduction root cause of concurrent test/module 
execution
colliding on ports, not Spring caching (those tests don't use Spring XML at 
all). {{camel-cxf-spring-soap}} sets {{{}reuseForks=false{}}}, so each class 
already gets its own fresh JVM — ruling out a within-module cross-class leak 
and leaving cross-{*}module{*} contention under {{mvnd.threads=2}} as the 
best-supported explanation.

*Proposed fix:* add {{@DirtiesContext(classMode = 
DirtiesContext.ClassMode.AFTER_CLASS)}} to {{WSAddressingTest}} only 
(subclasses {{{}WSAddressingPAYLOADTest{}}}/{{{}WSAddressingCXFMSGTest{}}} 
inherit it — verified against this exact pattern already in use in 
{{{}CamelSpringPlainTest{}}}). This directly fixes symptom 1. It doesn't make 
the underlying {{AvailablePortFinder}} race in symptom 2 impossible — that's a 
separate, pre-existing, project-wide issue — but it does mean each Surefire 
rerun (CI already runs with {{{}rerunFailingTestsCount=2{}}}) now gets a 
genuinely fresh, consistent context instead of a poisoned one, giving the 
existing rerun mechanism a real chance to recover from a transient 
cross-process bind collision.

Found ~10 other test classes in {{camel-cxf-spring-soap}} with the identical 
at-risk pattern ({{{}AvailablePortFinder{}}} + no {{{}@DirtiesContext{}}}): 
{{{}Camel1145RouteTest{}}}, {{{}LoggingInterceptorInMessageModeTest{}}}, 
{{{}ServiceClassRefTest{}}}, {{{}CxfMessageHeadersRelayTest{}}}, 
{{{}WSRMTest{}}}, and the {{CxfMtom*}} family. They haven't been reported 
flaky, so leaving them out of this fix to keep it minimal — may be worth a 
separate follow-up ticket to audit them.

_Claude Code on behalf of tmielke_

> Fix flaky test WSAddressingTest
> -------------------------------
>
>                 Key: CAMEL-27158
>                 URL: https://issues.apache.org/jira/browse/CAMEL-27158
>             Project: Camel
>          Issue Type: Test
>          Components: camel-cxf
>    Affects Versions: 4.24.0
>            Reporter: Aurélien Pupier
>            Assignee: Torsten Mielke
>            Priority: Major
>
> it has failed [3 times in the last 28 
> days|https://develocity.apache.org/scans/tests?search.relativeStartTime=P28D&search.rootProjectNames=camel&search.tags=main&search.timeZoneId=Europe%2FParis&tests.container=*WSAddressingTest]
> first run:
> {noformat}
> org.apache.cxf.interceptor.Fault: Could not start Undertow server on port 
> 44,703: java.net.BindException: Address already in use      
>       at 
> org.apache.cxf.transport.http_undertow.UndertowHTTPServerEngine.addServant(UndertowHTTPServerEngine.java:171)
>         
>       at 
> org.apache.cxf.transport.http_undertow.UndertowHTTPDestination.activate(UndertowHTTPDestination.java:154)
>     
>       at 
> org.apache.cxf.transport.AbstractObservable.setMessageObserver(AbstractObservable.java:53)
>    
>       at 
> org.apache.cxf.binding.AbstractBindingFactory.addListener(AbstractBindingFactory.java:95)
>     
>       at 
> org.apache.cxf.binding.soap.SoapBindingFactory.addListener(SoapBindingFactory.java:894)
>       
>       at org.apache.cxf.endpoint.ServerImpl.start(ServerImpl.java:130)        
>       at 
> org.apache.cxf.frontend.ServerFactoryBean.create(ServerFactoryBean.java:220) 
>       at 
> org.apache.cxf.jaxws.JaxWsServerFactoryBean.create(JaxWsServerFactoryBean.java:210)
>   
>       at 
> org.apache.camel.component.cxf.wsa.WSAddressingTest.setUp(WSAddressingTest.java:119)
>  
> Caused by: java.lang.RuntimeException: java.net.BindException: Address 
> already in use 
>       at io.undertow.Undertow.start(Undertow.java:254)        
>       at 
> org.apache.cxf.transport.http_undertow.UndertowHTTPServerEngine.addServant(UndertowHTTPServerEngine.java:161)
>         
>       at 
> org.apache.cxf.transport.http_undertow.UndertowHTTPDestination.activate(UndertowHTTPDestination.java:154)
>     
>       at 
> org.apache.cxf.transport.AbstractObservable.setMessageObserver(AbstractObservable.java:53)
>    
>       at 
> org.apache.cxf.binding.AbstractBindingFactory.addListener(AbstractBindingFactory.java:95)
>     
>       at 
> org.apache.cxf.binding.soap.SoapBindingFactory.addListener(SoapBindingFactory.java:894)
>       
>       at org.apache.cxf.endpoint.ServerImpl.start(ServerImpl.java:130)        
>       at 
> org.apache.cxf.frontend.ServerFactoryBean.create(ServerFactoryBean.java:220) 
>       at 
> org.apache.cxf.jaxws.JaxWsServerFactoryBean.create(JaxWsServerFactoryBean.java:210)
>   
>       at 
> org.apache.camel.component.cxf.wsa.WSAddressingTest.setUp(WSAddressingTest.java:119)
>  
> Caused by: java.net.BindException: Address already in use     
>       at sun.nio.ch.Net.bind0(Native Method)  
>       at sun.nio.ch.Net.bind(Net.java:522)    
>       at 
> sun.nio.ch.ServerSocketChannelImpl.netBind(ServerSocketChannelImpl.java:335) 
>       at 
> sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:297)    
>       at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:80)     
>       at 
> org.xnio.nio.NioXnioWorker.createTcpConnectionServer(NioXnioWorker.java:178) 
>       at 
> org.xnio.XnioWorker.createStreamConnectionServer(XnioWorker.java:310)        
>       at io.undertow.Undertow.start(Undertow.java:196)        
>       at 
> org.apache.cxf.transport.http_undertow.UndertowHTTPServerEngine.addServant(UndertowHTTPServerEngine.java:161)
>         
>       at 
> org.apache.cxf.transport.http_undertow.UndertowHTTPDestination.activate(UndertowHTTPDestination.java:154)
>     
>       at 
> org.apache.cxf.transport.AbstractObservable.setMessageObserver(AbstractObservable.java:53)
>    
>       at 
> org.apache.cxf.binding.AbstractBindingFactory.addListener(AbstractBindingFactory.java:95)
>     
>       at 
> org.apache.cxf.binding.soap.SoapBindingFactory.addListener(SoapBindingFactory.java:894)
>       
>       at org.apache.cxf.endpoint.ServerImpl.start(ServerImpl.java:130)        
>       at 
> org.apache.cxf.frontend.ServerFactoryBean.create(ServerFactoryBean.java:220) 
>       at 
> org.apache.cxf.jaxws.JaxWsServerFactoryBean.create(JaxWsServerFactoryBean.java:210)
>   
>       at 
> org.apache.camel.component.cxf.wsa.WSAddressingTest.setUp(WSAddressingTest.java:119)
> {noformat}
> reruns:
> {noformat}
> jakarta.xml.ws.WebServiceException: Could not send Message.   
>       at 
> org.apache.cxf.jaxws.JaxWsClientProxy.mapException(JaxWsClientProxy.java:183) 
>        
>       at 
> org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:145)      
>       at jdk.proxyXX.$ProxyXX.greetMe(Unknown Source) 
>       at 
> org.apache.camel.component.cxf.wsa.WSAddressingTest.testWSAddressing(WSAddressingTest.java:144)
>       
> Caused by: java.net.ConnectException: (No message provided)   
>       at 
> jdk.internal.net.http.common.Utils.toConnectException(Utils.java:1070)       
>       at 
> jdk.internal.net.http.PlainHttpConnection.connectAsync(PlainHttpConnection.java:223)
>  
>       at 
> jdk.internal.net.http.PlainHttpConnection.checkRetryConnect(PlainHttpConnection.java:277)
>     
>       at 
> jdk.internal.net.http.PlainHttpConnection.lambda$connectAsync$0(PlainHttpConnection.java:235)
>         
>       at 
> java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:955) 
>       at 
> java.util.concurrent.CompletableFuture$UniHandle.tryFire(CompletableFuture.java:932)
>  
>       at 
> java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:531)
>       
>       at 
> java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1794)
>   
>       at 
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1090)
>       
>       at 
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:614)
>       
>       at java.lang.Thread.run(Thread.java:1474)       
> Caused by: java.nio.channels.ClosedChannelException: (No message provided)    
>       at sun.nio.ch.SocketChannelImpl.ensureOpen(SocketChannelImpl.java:204)  
>       at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:942)     
>       at 
> jdk.internal.net.http.PlainHttpConnection.connectAsync(PlainHttpConnection.java:211)
>  
>       at 
> jdk.internal.net.http.PlainHttpConnection.checkRetryConnect(PlainHttpConnection.java:277)
>     
>       at 
> jdk.internal.net.http.PlainHttpConnection.lambda$connectAsync$0(PlainHttpConnection.java:235)
>         
>       at 
> java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:955) 
>       at 
> java.util.concurrent.CompletableFuture$UniHandle.tryFire(CompletableFuture.java:932)
>  
>       at 
> java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:531)
>       
>       at 
> java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1794)
>   
>       at 
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1090)
>       
>       at 
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:614)
>       
>       at java.lang.Thread.run(Thread.java:1474)
> {noformat}



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to