Skip to content

[Bug] Fix TOCTOU race condition in ZookeeperClientManager.getInstance()#16211

Open
LiyunZhang10 wants to merge 1 commit intoapache:3.3from
LiyunZhang10:fix-toctou-race-in-zookeeper-client-manager
Open

[Bug] Fix TOCTOU race condition in ZookeeperClientManager.getInstance()#16211
LiyunZhang10 wants to merge 1 commit intoapache:3.3from
LiyunZhang10:fix-toctou-race-in-zookeeper-client-manager

Conversation

@LiyunZhang10
Copy link
Copy Markdown

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 (containsKey followed by put) on a ConcurrentHashMap.

When multiple threads call getInstance() concurrently for the first time with the same ApplicationModel, each thread can bypass the containsKey check, create a separate ZookeeperClientManager instance, and register duplicate addDestroyListener callbacks. The later threads will overwrite the earlier instances in managerMap, 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?

  • Replaced the containsKey + put pattern with managerMap.computeIfAbsent() to ensure that exactly one manager instance and exactly one destroy listener are created per ApplicationModel, regardless of concurrency.
  • Added testGetInstanceConcurrentRaceCondition to verify that concurrent calls safely return exactly 1 instance and create only 1 entry in the map.

@LiyunZhang10 LiyunZhang10 force-pushed the fix-toctou-race-in-zookeeper-client-manager branch from 50a921d to 64e5692 Compare April 13, 2026 13:33
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
@LiyunZhang10 LiyunZhang10 force-pushed the fix-toctou-race-in-zookeeper-client-manager branch from 64e5692 to 6858d3f Compare April 14, 2026 01:50
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.

1 participant