Skip to content

CXF-9289: Serialize WebTarget.request() and give each request its own ClientProviderFactory - #3823

Open
rzo1 wants to merge 8 commits into
apache:mainfrom
rzo1:CXF-9289
Open

rzo1 wants to merge 8 commits into
apache:mainfrom
rzo1:CXF-9289

Conversation

@rzo1

@rzo1 rzo1 commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

https://issues.apache.org/jira/browse/CXF-9289

Concurrent WebTarget.request() calls on one target fail with a ConcurrentModificationException: every request() re-applied the client providers to the ClientProviderFactory of the endpoint, which is shared by all builders of the target and read by the invocations in flight. This makes the Jakarta REST TCK sseBroadcastTest flaky (5 SseEventSources opened in parallel on one target; the failing one silently never connects).

  • Own factory per request: WebTargetImpl.request() copies the ClientProviderFactory of 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 by request().
  • Lookup: the client binds its factory to each exchange it creates; ClientProviderFactory.getInstance(Message) and ProviderFactory.getInstance(Message) return the factory of the exchange, if there is one, and the factory of the endpoint otherwise.
  • Lock: request() still updates the shared ClientConfiguration under a lock that the derived targets inherit, which also makes the lazy targetClient initialization safe.
  • SSE: InboundSseEventProcessor reads 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.

ProviderFactory keeps 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#testRequestGetsItsOwnProviderFactory and ClientProviderFactoryTest#testCopyDoesNotShareItsProviderLists. cxf-rt-frontend-jaxrs (1169 tests), cxf-rt-rs-client (121), cxf-rt-rs-sse (32) and cxf-rt-rs-mp-client (45) build with all tests and checks.

…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 coheigea left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the diagnosis is right and the list changes look good. Two things:

  1. 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.
  2. 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.
@rzo1

rzo1 commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review, all addressed in the second commit:

  1. Interceptors: mapInterceptorFilters skips providers that are already mapped under the same name, and setCommonProviders adds new interceptors to a copy of the map that then replaces the (now volatile) field, so invocations iterate a stable snapshot. ProviderFactoryTest has two new tests: one re-applying the same interceptors (failed with 3 entries instead of 1), one adding interceptors while other threads build interceptor chains (failed with a ConcurrentModificationException). The client test registers a ReaderInterceptor and a WriterInterceptor too.
  2. Lock: request() now locks on the ClientConfiguration of the target client. Derived targets share it (also with USE_CONFIGURATION_REFERENCE_WHEN_COPY=false, which passes the same instance), while targets from separate client.target(...) calls don't wait on each other. The target client itself is created under a per-target lock, so createWebClient() is no longer under the configuration lock; Feature.initialize() still is, as features change that shared configuration.
  3. targetClient is volatile.

cxf-rt-frontend-jaxrs (1171 tests) and cxf-rt-rs-client (119 tests) build with all tests and checks.

@rzo1
rzo1 requested a review from coheigea December 4, 2026 10:12

@coheigea coheigea left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. 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.
  2. 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.
@rzo1

rzo1 commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, both nits addressed in the third commit: withInterceptors returns the current map without copying when all interceptors are already mapped, and request() locks on a private lock object that the targets derived via newWebTarget inherit, instead of on the ClientConfiguration.

@coheigea coheigea left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, both nits are addressed. LGTM once CI is green.

@reta Please take a look as well.

@reta

reta commented Dec 4, 2026

Copy link
Copy Markdown
Member

Thanks, both nits are addressed. LGTM once CI is green.

@reta Please take a look as well.

Thanks @rzo1 , I think the synchronization on ClientImpl looks good, but I believe ProviderFactory (and ClientProviderFactory) were never supposed to be used across threads. I am wondering if we could make copy instead of reusing it? So each client will get own instance, no reuse?

@rzo1

rzo1 commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @reta, that makes sense. Today the ClientProviderFactory lives on the endpoint, every WebTarget.request() re-applies the providers to it, and the Invocation.Builder it returns shares the same configuration and endpoint, so invocations in flight read a factory the next request() is changing. The PR keeps that sharing and makes it safe; a copy would remove it.

Before I rework it, which copy do you have in mind?

  1. Copy only the ClientProviderFactory per request() and bind it to the invocation, with configuration, endpoint and conduit still shared. The copy-on-write changes in ProviderFactory would go away; the lock in ClientImpl stays for the shared configuration.
  2. Create a new client (endpoint, configuration, conduit, factory) per request(). Nothing is shared then, but each request gets its own conduit, and customisations via WebClient.getConfig(target) would no longer reach later requests.

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.

@reta

reta commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Thanks @reta, that makes sense. Today the ClientProviderFactory lives on the endpoint, every WebTarget.request() re-applies the providers to it, and the Invocation.Builder it returns shares the same configuration and endpoint, so invocations in flight read a factory the next request() is changing. The PR keeps that sharing and makes it safe; a copy would remove it.

Before I rework it, which copy do you have in mind?

  1. Copy only the ClientProviderFactory per request() and bind it to the invocation, with configuration, endpoint and conduit still shared. The copy-on-write changes in ProviderFactory would go away; the lock in ClientImpl stays for the shared configuration.
  2. Create a new client (endpoint, configuration, conduit, factory) per request(). Nothing is shared then, but each request gets its own conduit, and customisations via WebClient.getConfig(target) would no longer reach later requests.

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 ClientProviderFactory is the issue there

…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.
@rzo1 rzo1 changed the title CXF-9289: Serialize WebTarget.request() and use copy-on-write provider lists CXF-9289: Serialize WebTarget.request() and give each request its own ClientProviderFactory Dec 4, 2026
@rzo1

rzo1 commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Done in the last commit: request() now copies the ClientProviderFactory, applies the providers to the copy and binds it to the invocation; the copy-on-write changes in ProviderFactory are reverted. The SSE client needed a small change to pick up the factory of the request.

…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.
@reta

reta commented Dec 4, 2026

Copy link
Copy Markdown
Member

Thanks, both nits are addressed. LGTM once CI is green.

@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()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants