spring-data-redis icon indicating copy to clipboard operation
spring-data-redis copied to clipboard

Fix redundant disposal in LettucePoolingConnectionProvider by implementing SmartLifecycle #3100

Open mayleaf opened this issue 6 months ago • 1 comments

Enhancements to LettuceConnectionFactory:

  • Added proper disposal of ClusterCommandExecutor during the stop method to ensure resources are released cleanly.
  • Removed redundant cleanup logic for ClusterCommandExecutor from the destroy method, as it is now handled in the stop method.
  • Introduced a new private dispose method to encapsulate the cleanup logic for ClusterCommandExecutor, improving code readability and maintainability.

Enhancements to LettucePoolingConnectionProvider:

  • Implemented SmartLifecycle to manage the lifecycle of the connection provider, including start, stop, and isRunning methods.
  • Added an AtomicReference-based State enum to track the lifecycle state of the connection provider, ensuring thread-safe transitions between states. These updates improve the robustness and maintainability of the connection handling logic in the Spring Data Redis project.

I have found out that #3100 was reproduced when redis run with cluster mode.

LettuceConnectionFactory uses a connectionProvider that refered at ClusterCommandExecuter. When redis run with Cluster mode, it uses LettucePoolingConnectionProvider, which only implements DisposableBean.

When the LettuceConnectionFactory disposed, it disposes ConnectionProvider first, and disposes ClusterCommandExecuter next.

And when the ClusterCommandExecuter disposed, it disposes the connectionProvider - shared with LettuceConnectionFactory. As a result, LettucePoolingConnectionProvider's destroy method called twice.

Finally, the LettucePoolingConnectionProvider tried releasing connections in it's pool - that has 0 connections.

To solve this issue, I've implemented SmartLifeCycle at LettucePoolingConnectionProvider, and prevent duplicated stop method execution by AtomicReference based state management. Solved #3100, See #3103

  • [x] You have read the Spring Data contribution guidelines.
  • [x] You use the code formatters provided here and have them applied to your changes. Don’t submit any formatting related changes.
  • [ ] You submit test cases (unit or integration tests) that back your changes.
  • [x] You added yourself as author in the headers of the classes you touched. Amend the date range in the Apache license header if needed. For new types, add the license header (copy from another file and set the current year only).

mayleaf avatar May 29 '25 09:05 mayleaf

Hi @mp911de, PTAL at the following comment when you get a chance. Thanks! 😊

mayleaf avatar Jun 23 '25 09:06 mayleaf

Thank you for your contribution. That's merged, polished, and backported now.

mp911de avatar Jul 02 '25 06:07 mp911de