LiyunZhang10 opened a new pull request, #16211:
URL: https://github.com/apache/dubbo/pull/16211

   ## 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.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to