Repository navigation
Conversation
…r lists WebTarget.request() re-applies the client providers to the provider factory shared by all builders of the target, which adds to and re-sorts its plain ArrayLists. Concurrent request() calls on one WebTarget (e.g. several SseEventSources opened in parallel) failed with a ConcurrentModificationException. request() now configures under a client-level lock, the provider lists are CopyOnWriteArrayLists so that in-flight invocations iterate a stable snapshot, and ParamConverterProviders are no longer added again on every request().
coheigea
left a comment
There was a problem hiding this comment.
Thanks, the diagnosis is right and the list changes look good. Two things:
- Reader/writer interceptors still have the race and still leak. On every request(), mapInterceptorFilters calls put(new NameKey(...)) on the readerInterceptors/writerInterceptors TreeMaps. NameKey uses the default hashCode(), and NameKeyComparator falls back to comparing it, so every call adds a new entry. The maps grow on each request(), and while that's happening, getBoundFilters iterates them for in-flight calls without the lock, so it can still throw a CME. Could you skip providers that are already mapped, and either rebuild-and-swap the map (like setProviderComparator does) or use a ConcurrentSkipListMap? Registering a ReaderInterceptor/WriterInterceptor in the new test would cover this.
- The lock is client-wide. Targets created by separate client.target(...) calls have their own provider factory, but they now wait on each other. The lock is also held during createWebClient() and Feature.initialize(). Could it be per target, or on the shared ClientConfiguration? If you'd rather keep it client-wide, please note in the comment that it serializes all targets of the client.
Nit: targetClient is read without the lock in getWebClient(), so it should probably be volatile.
…t configuration Re-applying the providers on every WebTarget.request() added another entry to the reader/writer interceptor maps (NameKey uses the identity hash code), and invocations iterating those maps could still fail with a ConcurrentModificationException. Interceptors that are already mapped are now skipped, and new ones go into a copy of the map that replaces it. request() now locks on the ClientConfiguration of the target client, which is shared by the targets derived from it, instead of on the whole client; the target client is created under a lock of its own, and targetClient is volatile.
|
Thanks for the review, all addressed in the second commit:
|
There was a problem hiding this comment.
Thanks, that addresses everything. The snapshot-and-swap for the interceptor maps and the lock on the ClientConfiguration both look right. LGTM once CI is green.
Small nits:
- withInterceptors copies the map on every request() even when nothing new is added, which is the normal case on the client. Checking isMapped against the current map before copying would avoid the allocation.
- synchronized (clientCfg) locks on an object other code can reach. A dedicated lock would be safer.
… a dedicated lock The client re-applies the same providers on every request(), so the interceptor maps are only copied when an interceptor is not mapped yet. request() locks on a private lock the derived targets inherit instead of on the ClientConfiguration, which other code can reach.
|
Thanks, both nits addressed in the third commit: |
Thanks @rzo1 , I think the synchronization on ClientImpl looks good, but I believe |
|
Thanks @reta, that makes sense. Today the Before I rework it, which copy do you have in mind?
I would go for 1, as it does not change connection handling or existing client configuration. Happy to do it in this PR if you agree. |
Thanks @rzo1 , definitely 1 was on my mind, it seems like the |
…viderFactory The ClientProviderFactory of the endpoint is shared by all requests of a target and of the targets derived from it, and was not meant to be changed while invocations read it from other threads. request() now copies it, applies the providers of the configuration to the copy and binds the copy to the client of the invocation, which puts it on its exchanges. The factory lookups by message return the factory of the exchange, if there is one, and the factory of the endpoint otherwise. The provider lists and interceptor maps of ProviderFactory are plain collections again, as the shared factory is no longer changed by request(). The lock in ClientImpl stays for the shared ClientConfiguration. The SSE client reads the events with the factory of the request instead of the one of the endpoint, so that it still sees the providers of the target.
|
Done in the last commit: |
…ad of the exchange An exchange can be reused for another request, for example on a retry, so the factory is put on the request message. The lookups read it from the out message of the exchange, which also covers the response.
The copy only reads the factory of the endpoint, which nothing changes after the target client is created, so only the update of the shared ClientConfiguration stays under the lock.
@coheigea we changed the approach a bit, if you want to take a second look, thanks! |
…ovider factory configureRequest() reads the CXF features from the configuration itself. The copy of the interceptor maps needs no fallback, they are sorted maps.
| // CXF Features | ||
| for (org.apache.cxf.feature.Feature cxfFeature : cxfFeatures) { | ||
| cxfFeature.initialize(clientCfg, clientCfg.getBus()); | ||
| for (Object p : configImpl.getConfiguration().getInstances()) { |
https://issues.apache.org/jira/browse/CXF-9289
Concurrent
WebTarget.request()calls on one target fail with aConcurrentModificationException: everyrequest()re-applied the client providers to theClientProviderFactoryof the endpoint, which is shared by all builders of the target and read by the invocations in flight. This makes the Jakarta REST TCKsseBroadcastTestflaky (5SseEventSources opened in parallel on one target; the failing one silently never connects).WebTargetImpl.request()copies theClientProviderFactoryof the endpoint (ClientProviderFactory.copy()), applies the providers of the configuration to the copy and sets it on the client of the invocation. The factory of the endpoint is no longer changed byrequest().ClientProviderFactory.getInstance(Message)andProviderFactory.getInstance(Message)return the factory of the exchange, if there is one, and the factory of the endpoint otherwise.request()still updates the sharedClientConfigurationunder a lock that the derived targets inherit, which also makes the lazytargetClientinitialization safe.InboundSseEventProcessorreads the events with the factory of the request instead of the one of the endpoint, so it still sees the providers registered on the target.ProviderFactorykeeps its plain lists and maps; the copy-on-write collections of the first commits are gone (see the discussion with @reta below).Tests:
ClientImplTest#testRequestOnSharedTargetConcurrently(fails without the change),ClientImplTest#testRequestGetsItsOwnProviderFactoryandClientProviderFactoryTest#testCopyDoesNotShareItsProviderLists.cxf-rt-frontend-jaxrs(1169 tests),cxf-rt-rs-client(121),cxf-rt-rs-sse(32) andcxf-rt-rs-mp-client(45) build with all tests and checks.