Skip to content

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

Open
LiyunZhang10 wants to merge 4 commits 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 4 commits 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
@zrlw
Copy link
Copy Markdown
Contributor

zrlw commented Apr 23, 2026

run mvn spotless:apply to format your pr codes

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 23, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 60.81%. Comparing base (21cd2ca) to head (0d94cb9).
⚠️ Report is 23 commits behind head on 3.3.

Additional details and impacted files
@@             Coverage Diff              @@
##                3.3   #16211      +/-   ##
============================================
+ Coverage     60.80%   60.81%   +0.01%     
- Complexity    11764    11765       +1     
============================================
  Files          1953     1953              
  Lines         89182    89181       -1     
  Branches      13453    13453              
============================================
+ Hits          54224    54233       +9     
+ Misses        29388    29382       -6     
+ Partials       5570     5566       -4     
Flag Coverage Δ
integration-tests-java21 32.17% <100.00%> (+0.04%) ⬆️
integration-tests-java8 32.23% <100.00%> (+0.03%) ⬆️
samples-tests-java21 32.19% <100.00%> (+0.01%) ⬆️
samples-tests-java8 29.75% <100.00%> (-0.03%) ⬇️
unit-tests-java11 58.99% <100.00%> (-0.01%) ⬇️
unit-tests-java17 58.49% <100.00%> (+<0.01%) ⬆️
unit-tests-java21 58.51% <100.00%> (+0.04%) ⬆️
unit-tests-java25 58.40% <100.00%> (-0.08%) ⬇️
unit-tests-java8 59.05% <100.00%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Contributor

@zrlw zrlw left a comment

Choose a reason for hiding this comment

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

LGTM

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants