Freeman Yue Fang created CXF-9265:
-------------------------------------

             Summary: Closing or finalizing a JAX-RS client proxy closes the 
conduit of its sub-resource proxies
                 Key: CXF-9265
                 URL: https://issues.apache.org/jira/browse/CXF-9265
             Project: CXF
          Issue Type: Bug
          Components: JAX-RS
    Affects Versions: 4.2.4, 4.1.9
            Reporter: Freeman Yue Fang


When a JAX-RS client proxy returns a sub-resource proxy, both proxies use the 
same ClientConfiguration, and so the same conduit. But ClientProxyImpl hands 
the configuration to the sub-resource proxy with setConfiguration(), which 
starts a new configuration reference with a count of 1, instead of sharing the 
parent's reference:

{code:java}
proxyImpl.setConfiguration(getConfiguration());
{code}

So each proxy closes the shared conduit as if it were the only one using it. 
With the default HttpClientHTTPConduit, closing the conduit releases the shared 
java.net.http.HttpClient, which is shut down when its count reaches 0. Calls in 
flight then fail with:

{noformat}
jakarta.ws.rs.ProcessingException: java.io.IOException: shutdownNow
{noformat}

(on JDK 17 and older: "java.io.IOException: selector manager closed").

h3. Two ways to hit it

# *Closing the parent proxy:* a call on the sub-resource proxy fails if the 
parent proxy is closed while the call is in flight, or before it starts.
# *Finalization:* an application that drops both proxies without closing them, 
for example {{JAXRSClientFactory.create(address, Root.class).sub().name()}}, 
leaves both for the finalizer, and both finalize() calls close the same 
conduit. When the finalizer runs on two threads (System.runFinalization() 
starts a second one), both threads can run HttpClientHTTPConduit.close() at the 
same time and release the shared HttpClient twice. The count then reaches 0 
while other conduits still use the HttpClient, and calls of unrelated clients 
fail with the same exception.

h3. Reproducer

A local HttpServer runs an action while each call is in flight. On JDK 21 with 
CXF 4.2.4 and 4.1.9:
||Test||What happens during the call||Calls that succeed||
|Close the root proxy|the root proxy that returned the sub-resource proxy is 
closed|0 of 10|
|Finalized proxies|dropped root and sub-resource proxies are finalized|26 to 29 
of 30|
|Control: WebClient.fromClient()|the WebClient that the copy came from is 
closed|10 of 10|

The control passes because CXF-8992 already fixed the same problem for 
WebClient.fromClient(): the copy shares the original's configuration reference. 
Sub-resource proxies were not changed. The 3.6.x line has the same code.

h3. Proposed fix

* ClientProxyImpl passes the configuration reference to the sub-resource proxy, 
as WebClient.copyProperties() does:
{code:java}
proxyImpl.setConfigurationReference(getConfigurationReference());
{code}
This fixes both cases: the conduit is closed once, when the last proxy that 
uses it is closed.
* Hardening: HttpClientHTTPConduit.close() takes and clears its HttpClient 
reference under a lock and releases it at most once, so a conduit closed twice 
can't release a shared HttpClient that other conduits still use.



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

Reply via email to