[Bug] Fix TOCTOU race condition in ZookeeperClientManager.getInstance()#16211
Open
LiyunZhang10 wants to merge 1 commit intoapache:3.3from
Open
[Bug] Fix TOCTOU race condition in ZookeeperClientManager.getInstance()#16211LiyunZhang10 wants to merge 1 commit intoapache:3.3from
LiyunZhang10 wants to merge 1 commit intoapache:3.3from
Conversation
50a921d to
64e5692
Compare
ZookeeperClientManager.getInstance() uses a non-atomic check-then-act pattern (containsKey + put) on a ConcurrentHashMap. When multiple threads call getInstance() concurrently with the same ApplicationModel, each thread can create a separate ZookeeperClientManager instance and register duplicate destroy listeners, causing redundant Zookeeper client cleanup and potential resource waste. Fix: replace the containsKey/put pattern with computeIfAbsent to ensure exactly one manager instance and one destroy listener per ApplicationModel. Added testGetInstanceConcurrentRaceCondition to verify exactly 1 instance is created across concurrent threads. Made-with: Cursor
64e5692 to
6858d3f
Compare
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.
What is the purpose of the change?
Fixes a Time-Of-Check to Time-Of-Use (TOCTOU) race condition in
ZookeeperClientManager.getInstance().In the current implementation,
getInstance(ApplicationModel)uses a non-atomic check-then-act pattern (containsKeyfollowed byput) on aConcurrentHashMap.When multiple threads call
getInstance()concurrently for the first time with the sameApplicationModel, each thread can bypass thecontainsKeycheck, create a separateZookeeperClientManagerinstance, and register duplicateaddDestroyListenercallbacks. The later threads will overwrite the earlier instances inmanagerMap, causing the overwritten instances to be lost but their destroy listeners to remain registered, which leads to redundant Zookeeper client cleanup operations and resource waste.What is changed?
containsKey+putpattern withmanagerMap.computeIfAbsent()to ensure that exactly one manager instance and exactly one destroy listener are created perApplicationModel, regardless of concurrency.testGetInstanceConcurrentRaceConditionto verify that concurrent calls safely return exactly 1 instance and create only 1 entry in the map.