[fix][client] Prevent duplicate ServiceUrlProvider initialization#25899
Open
oneby-wang wants to merge 1 commit into
Open
[fix][client] Prevent duplicate ServiceUrlProvider initialization#25899oneby-wang wants to merge 1 commit into
oneby-wang wants to merge 1 commit into
Conversation
void-ptr974
suggested changes
May 31, 2026
Contributor
There was a problem hiding this comment.
I think this needs a lifecycle fix. If the same ServiceUrlProvider instance is reused to build a second client, the second build now fails with IllegalStateException, but the constructor failure path calls shutdown(), which unconditionally closes conf.getServiceUrlProvider(). That can close the provider still used by the first live client.
Example:
ServiceUrlProvider provider = AutoClusterFailover.builder()
.primary(primary)
.secondary(List.of(secondary))
.failoverDelay(1, TimeUnit.SECONDS)
.switchBackDelay(1, TimeUnit.SECONDS)
.build();
PulsarClient client1 = PulsarClient.builder()
.serviceUrlProvider(provider)
.build();
PulsarClient.builder()
.serviceUrlProvider(provider)
.build(); // fails, then closes provider used by client1
Could we only close the provider on constructor failure if this PulsarClientImpl successfully initialized it?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
#25892 fixed a flaky
SameAuthParamsLookupAutoClusterFailoverTestby removing an extra manualfailover.initialize(client)call from the test.The root cause of that flakiness was duplicate initialization.
PulsarClientBuilder.build()already initializes the configuredServiceUrlProviderthroughPulsarClientImpl, so callinginitialize(client)again starts duplicate background checks for the same provider instance.This is especially problematic for
SameAuthParamsLookupAutoClusterFailover, because eachinitializecall creates a newbroker-service-url-checkEventLoopGroup. Multiple checker threads can then mutate the same failover state and produce subtle race conditions that are difficult to diagnose.AutoClusterFailoverandControlledClusterFailoverhave the same lifecycle risk: duplicate initialization can register duplicate scheduled tasks, andControlledClusterFailovercan also recreate its HTTP client without closing the previous one.Modifications
Verifying this change
Does this pull request potentially affect one of the following parts:
The threading model is affected only by preventing duplicate background failover check tasks from being registered for the same ServiceUrlProvider instance.