Fix wrong index in connection cleanup loops (MySQL and PgSQL)#5344
Fix wrong index in connection cleanup loops (MySQL and PgSQL)#5344renecannao wants to merge 1 commit intov3.0from
Conversation
This commit fixes a bug where connection cleanup loops were removing the wrong connection from the pool. The loops checked each connection by index (i), but when an expired connection was found, they removed index 0 instead of index i. This caused: - Fresh connections at index 0 to be incorrectly deleted - Expired connections to remain in the pool Fixed files: - lib/Base_HostGroups_Manager.cpp:2603 (MySQL) - lib/MySQL_HostGroups_Manager.cpp:2939 (MySQL) - lib/PgSQL_HostGroups_Manager.cpp:2782 (PgSQL) The fix changes `remove(0)` to `remove(i)` to remove the correct connection. Related: #5094 (fixes similar issue in drop_all_idle_connections)
📝 WalkthroughWalkthroughThe pull request updates the idle connection removal logic in three host group manager classes. Instead of always removing the first idle connection (index 0) when aging out expired connections, the code now removes the connection at the current loop index, maintaining proper array indexing with a corresponding decrement after removal. Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary of ChangesHello @renecannao, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical bug within the connection cleanup mechanisms for MySQL and PostgreSQL host group managers. Previously, the system incorrectly removed the first connection in the pool when an expired connection was identified, rather than removing the expired one itself. This correction ensures that only truly expired connections are removed, preventing potential connection pool corruption and resource leaks. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
There was a problem hiding this comment.
Code Review
This pull request correctly fixes a critical bug in the connection cleanup logic for both MySQL and PgSQL. By removing the connection at the correct index i instead of always at index 0, you've prevented a source of connection pool corruption and resource leaks. The fix is clear and directly addresses the issue. I have a couple of suggestions regarding code duplication that could improve maintainability.
| intv *= 1000; | ||
| if (curtime > mc->creation_time + intv) { | ||
| mc=mscl->remove(0); | ||
| mc=mscl->remove(i); |
There was a problem hiding this comment.
This change is inside a large #if 0 block, which means this code is not being compiled and the change has no effect. This block appears to be a duplicated, outdated implementation of MySQL_HostGroups_Manager::drop_all_idle_connections(). To prevent confusion and improve maintainability, it would be best to remove this dead code.



Summary
This PR fixes a bug in connection cleanup loops where the wrong connection was being removed from the pool. The loops iterate through connections by index
iand check each one usingindex(i), but when an expired connection was found, they were removing index0instead of indexi.Files Changed
lib/Base_HostGroups_Manager.cppremove(0)→remove(i)lib/MySQL_HostGroups_Manager.cppremove(0)→remove(i)lib/PgSQL_HostGroups_Manager.cppremove(0)→remove(i)Impact of the Bug
When the first connection (index 0) was fresh but a later connection was expired:
Example Scenario
5 connections in the pool, with connections at indices 2 and 4 expired:
Related PRs
drop_all_idle_connections()(MySQL)connection_max_agecleanup code for both MySQL and PgSQLTest plan
Summary by CodeRabbit
Release Notes