ABin-Huang opened a new pull request, #16450: URL: https://github.com/apache/dubbo/pull/16450
## What is the purpose of the change? Fix #8342. When `threadpool=cached` is configured with an explicit positive `corethreads` (e.g. `corethreads=2, threads=5, alive=15000`), idle **core** threads are never reclaimed, so the pool can never shrink below `corePoolSize`, even when every thread stays idle far longer than the configured keep-alive (`alive`). This contradicts the contract documented on `CachedThreadPool` — "Thread will be recycled after idle for one minute" — and the behavior of `java.util.concurrent.Executors#newCachedThreadPool()` that it is modeled on. ### Root cause `CachedThreadPool#getExecutor` builds a JDK `ThreadPoolExecutor` but never calls `allowCoreThreadTimeOut(true)`. By default a `ThreadPoolExecutor` does not time out core threads while they wait on the queue, so with `corethreads > 0` those core threads stay alive forever. ### Fix Call `executor.allowCoreThreadTimeOut(true)` for the cached pool so idle core threads are also reclaimed once they have been idle for `alive`. - The default cached pool uses `corethreads = 0`, so the default behavior is unchanged. - `fixed` / `limited` / `eager` pools are intentionally left untouched and keep their current (non-shrinking) semantics. ### Tests - Added `CachedThreadPoolTest#idleCoreThreadsShouldBeRecycledAfterKeepAlive`: grow the pool to `maximumPoolSize`, release all tasks and assert the pool shrinks back to 0 after keep-alive. The test fails (times out) before this fix and passes after. - Added an `allowsCoreThreadTimeOut()` assertion to `getExecutor1`. - The whole `org.apache.dubbo.common.threadpool` test package passes locally (41 tests, 0 failures/errors); spotless formatting applied. ## Checklist - [x] Make sure there is a [GitHub_issue](https://github.com/apache/dubbo/issues) field for the change. (#8342) - [x] Write a pull request description that is detailed enough to understand what the pull request does, how, and why. - [x] Write necessary unit-test to verify your logic correction. If the new feature or significant change is committed, please remember to add sample in [dubbo samples](https://github.com/apache/dubbo-samples) project. - [x] Make sure gitHub actions can pass. [Why the workflow is failing and how to fix it?](../CONTRIBUTING.md) -- 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]
