Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CURATOR-710: Fix leaking watch in EnsembleTracker #508

Merged

Conversation

kezhuw
Copy link
Member

@kezhuw kezhuw commented Oct 16, 2024

CURATOR-667(#474) fixes asynchronous event path for getConfig to
"/zookeeper/config" by using CuratorFramework::usingNamespace(null) to
fetch data.

It causes watcher not registering to possible WatcherRemovalManager,
so leaking in WatcherRemoveCuratorFramework::removeWatchers.

@kezhuw kezhuw force-pushed the CURATOR-710-fix-EnsembleTracker-leak-watch branch from a6319f0 to b74abb0 Compare October 28, 2024 08:10
CURATOR-667(apache#474) fixes asynchronous event path for `getConfig` to
"/zookeeper/config" by using `CuratorFramework::usingNamespace(null)` to
fetch data.

It causes watcher not registering to possible `WatcherRemovalManager`,
so leaking in `WatcherRemoveCuratorFramework::removeWatchers`.
@kezhuw kezhuw force-pushed the CURATOR-710-fix-EnsembleTracker-leak-watch branch from b74abb0 to cd03c72 Compare October 28, 2024 08:14
@kezhuw kezhuw requested review from eolivelli and tisonkun November 2, 2024 15:48
Comment on lines 66 to 69
Watching setWatcherRemovalManager(WatcherRemovalManager watcherRemovalManager) {
this.watcherRemovalManager = watcherRemovalManager;
return this;
}
Copy link
Member

Choose a reason for hiding this comment

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

It seems the watcherRemovalManager already set in the constructor by this.watcherRemovalManager = client.getWatcherRemovalManager();?

Copy link
Member

Choose a reason for hiding this comment

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

Or otherwise we can merge this setter into the constructor as a parameter.

if (client.getWatcherRemovalManager() != null) {
client.getWatcherRemovalManager().add(namespaceWatcher);
}
if (doCommit && namespaceWatcher != null && watcherRemovalManager != null) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you elaborate a bit when watcherRemovalManager can be different from client.getWatcherRemovalManager().

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with the question: we can save some memory by not having the field. The performance impact is negligible

Copy link
Member Author

Choose a reason for hiding this comment

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

  1. Given a fresh new CuratorFrameworkImpl, say framework0.
  2. Let's assume framework1 = framework0.newWatcherRemoveCuratorFramework(). framework1.getWatcherRemovalManager will be the WatcherRemovalManager.
  3. Let's assume framework2 = frame1.usingNamespace(null). framework2 will loss WatcherRemovalManager from above.
  4. framework1.removeWatchers will not drop watchers from new Watching(framework2, ...).

Step.2 is what exactly EnsembleTracker does.

EnsembleTracker(CuratorFramework client, EnsembleProvider ensembleProvider) {
this.client = client.newWatcherRemoveCuratorFramework();
this.ensembleProvider = ensembleProvider;
}

Step.3 is what exactly GetConfigBuilderImpl(CURATOR-667(#474)) does currently.

public GetConfigBuilderImpl(CuratorFrameworkImpl client) {
this.client = (CuratorFrameworkImpl) client.usingNamespace(null);
backgrounding = new Backgrounding();
watching = new Watching(this.client);
}

Step.4 is where this bug emerges.

public void close() {
if (state.compareAndSet(State.STARTED, State.CLOSED)) {
client.removeWatchers();
client.getConnectionStateListenable().removeListener(connectionStateListener);
}
}

I have pushed a fixup commit 6b78a3b with comments to doc this.

Copy link
Member

Choose a reason for hiding this comment

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

I get the point now. Let me check the code to see if we can have other less stateful/mutable solution 😆

Copy link
Member

@tisonkun tisonkun Jan 16, 2025

Choose a reason for hiding this comment

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

Here is a patch to avoid the later set kezhuw#1.

And I agree that we need some effort for #517 to keep the inheritance hierarchy easier to discover and extend.

Copy link
Member

Choose a reason for hiding this comment

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

Briefly, the root cause is WatcherRemovalFacade::usingNamespace drop the removalManager. Then by convey the manager in the method, things should work well.

Copy link
Member Author

Choose a reason for hiding this comment

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

I saw the patch. I think it is what I am trying to express in #517 (comment).

whether CuratorFramework::usingNamespace should inherit functionalities of this ?

It changes the behavior of CuratorFramework::usingNamespace, but I am ok.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah .. I suppose it's more like a bug to fix, since CURATOR-710 indicates that it's a bug anyway.

To provide an exit gate, perhaps we can add a new method to return a "basic CuratorFramework" to drop the removalManager explicitly, just like usingNamespace(null) to drop the namespace in facade. But we can leave it to #517.

@tisonkun
Copy link
Member

Is this PR ready for review now? @kezhuw

@kezhuw kezhuw closed this Dec 19, 2024
@kezhuw kezhuw reopened this Dec 19, 2024
@kezhuw
Copy link
Member Author

kezhuw commented Dec 19, 2024

@tisonkun It is ready. I have closed and reopened to revive ci.

tisonkun and others added 2 commits January 17, 2025 00:12
@kezhuw kezhuw merged commit fb78e23 into apache:master Jan 19, 2025
10 checks passed
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