allthingssecurity commented on PR #30623: URL: https://github.com/apache/camel/pull/30623#issuecomment-7094991975
One more commit after a red-team of 07a7c6fe6f, as the lock split was not complete: af189ae690. The LRU cache calls `onEvict` while it holds its write lock, and `SinglePool.evict` called `stopEvicted` from there. So for an idle evicted producer, the swap, the stop of the producer and the in-use check plus endpoint stop all ran under the cache's write lock. Two problems with that: 1. Deadlock. `ServicePool.stop()` holds the lock of the pool (`BaseService.stop`) and clears the cache in `doStop`, which needs the write lock. An acquire whose `putIfAbsent` evicts an idle singleton producer holds the write lock and waits for the pool lock in `takeService`. A stop of the producer cache (or a `purge()` from JMX) while sends are running can hang both threads. Main has the same lock order only on the "pool map larger than the capacity" path of `onEvict`; the first commit of this PR made it happen on every eviction of an idle singleton producer. 2. A slow stop of the evicted producer (your SFTP example) no longer held the pool lock after 07a7c6fe6f, but it still held the cache's write lock, and every acquire of that cache takes the read lock in `putIfAbsent`. So it blocked every acquire, not only the creations. The fix: `SinglePool.evict` only marks the pool evicted (as before) and queues it; `ServicePool.acquire` (after `putIfAbsent`) and `cleanUp()` (after the LRU cleanup) then call `stopEvicted` for the queued pools, outside the cache lock. The thread that evicts is the one that did the cache operation, so the evicted idle producer is still stopped in the same acquire, just after the cache lock is released. A pool still in use is stopped by its last release, as before. Tests: new `SinglePoolEvictionLockTest` (camel-core, latches, the `onSingleAcquired` hook and a producer whose stop waits): `testStopOfThePoolDoesNotDeadlockWithAnAcquireThatEvicts` and `testSlowStopOfAnEvictedProducerDoesNotBlockOtherAcquires`. Both fail on 07a7c6fe6f (the first deadlocks, the second's acquire waits for the stop). The test helper `MyServicePool.evict` in `ProducerCacheSingletonEvictionTest` now calls `cleanUp()` after `onEvict`, as the cache operation is followed by the drain. Model: the eviction now queues the pool and the thread drains the queue after the eviction loop, once the write lock is free (any thread may drain a pool another one queued). No violation: 2 threads, 2 singleton endpoints, cache size 1: 7,195,123 distinct states (8,810,731 with the fast path, 3,602,884 with one singleton and one non-singleton endpoint), everystate. A variant that never drains leaves an evicted pool behind (NoEvictedLeft). The model has no stop of the whole `ServicePool`, so it does not show the deadlock; the test does. Runs: on 07a7c6fe6f both new tests fail; with the change the three classes (SinglePoolEvictionLockTest, SinglePoolAcquireEvictionTest, ProducerCacheSingletonEvictionTest) 20 times in one JVM: 200 run, 0 failures. camel-support 181 run, 0 failures, 5 skipped (and 181 in its virtual threads run); the same camel-core set as before: 933 run, 0 failures, 2 skipped. -- 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]
