Richard Zowalla created CXF-9289:
------------------------------------

             Summary: Concurrent WebTarget.request() calls corrupt the shared 
client provider lists (ConcurrentModificationException)
                 Key: CXF-9289
                 URL: https://issues.apache.org/jira/browse/CXF-9289
             Project: CXF
          Issue Type: Bug
            Reporter: Richard Zowalla


Using one JAX\-RS client {{WebTarget}} from several threads at once is not 
safe. {{ClientImpl.WebTargetImpl.request\(\)}} calls 
{{initTargetClientIfNeeded\(\)}} and 
{{ClientProviderFactory.setUserProviders\(...\)}} on every call. That adds to 
and re\-sorts \({{ArrayList.sort}}\) the provider lists of {{ProviderFactory}} 
and {{ClientProviderFactory}} \(message readers/writers, context resolvers, 
param converters, filters, ...\). These are plain {{ArrayList}}s shared by 
every builder of the target, so concurrent {{request\(\)}} calls fail with:

{noformat}
java.util.ConcurrentModificationException
    at java.util.ArrayList.sort
    at org.apache.cxf.jaxrs.provider.ProviderFactory.sortReaders
    at org.apache.cxf.jaxrs.provider.ProviderFactory.setCommonProviders
    at org.apache.cxf.jaxrs.provider.ProviderFactory.setUserProviders
    at org.apache.cxf.jaxrs.client.spec.ClientImpl$WebTargetImpl.request
    at org.apache.cxf.jaxrs.sse.client.SseEventSourceImpl.connect
{noformat}

A rarer variant fails in {{ArrayList$Itr.next}} <\- 
{{ProviderFactory.createMessageBodyReader}} when one thread reads the lists for 
an in\-flight response while another re\-sorts them.

This makes the Jakarta REST TCK test 
{{ee.jakarta.tck.ws.rs.jaxrs21.ee.sse.ssebroadcaster.JAXRSClientIT#sseBroadcastTest}}
 flaky in Apache TomEE: it opens 5 {{SseEventSource}}s in parallel on one 
shared {{WebTarget}}. {{SseEventSourceImpl.connect\(\)}} hands the exception to 
{{onError}}, which the TCK does not register, so one client silently never 
connects \("Received unexpected number of events 0"\). A standalone 
reproduction with 5 threads opening {{SseEventSource}}s on one target fails in 
about 5\-10% of iterations.

Proposed fix:
\* {{WebTargetImpl.request\(\)}} configures the request under a client\-level 
lock \(targets derived via {{path\(\)}} share the configuration and provider 
factory\), which also makes the lazy {{targetClient}} initialization safe.
\* The provider lists become {{CopyOnWriteArrayList}}: they are read on every 
invocation and written rarely, so re\-sorting is atomic and readers iterate a 
stable snapshot.
\* {{setCommonProviders}} adds {{ParamConverterProvider}}s through 
{{addProviderToList}} like all other provider lists. Today every 
{{request\(\)}} appends the same instance again, so the list grows by one entry 
per request.

With the fix: 0 failures in 300 iterations of the reproduction \(6/100 failed 
before\), and the param converter list stays at one entry after 100 
{{request\(\)}} calls \(100 before\).

Affects at least 4.1.x and 4.2.x and current main.



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

Reply via email to