Skip to content

Fix wrong index in connection cleanup loops (MySQL and PgSQL)#5344

Open
renecannao wants to merge 1 commit intov3.0from
fix/remove-wrong-index-connection-cleanup
Open

Fix wrong index in connection cleanup loops (MySQL and PgSQL)#5344
renecannao wants to merge 1 commit intov3.0from
fix/remove-wrong-index-connection-cleanup

Conversation

@renecannao
Copy link
Contributor

@renecannao renecannao commented Feb 5, 2026

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 i and check each one using index(i), but when an expired connection was found, they were removing index 0 instead of index i.

Files Changed

File Line Issue
lib/Base_HostGroups_Manager.cpp 2603 remove(0)remove(i)
lib/MySQL_HostGroups_Manager.cpp 2939 remove(0)remove(i)
lib/PgSQL_HostGroups_Manager.cpp 2782 remove(0)remove(i)

Impact of the Bug

When the first connection (index 0) was fresh but a later connection was expired:

  • The fresh connection at index 0 was deleted
  • The expired connection remained in the pool
  • This could cause connection pool corruption and resource leaks

Example Scenario

5 connections in the pool, with connections at indices 2 and 4 expired:

Index Before Fix After Fix
0 (Fresh) ❌ Deleted ✅ Kept
1 (Fresh) ✅ Kept ✅ Kept
2 (Expired) ✅ Kept (BUG!) ❌ Deleted
3 (Fresh) ✅ Kept ✅ Kept
4 (Expired) ✅ Kept (BUG!) ❌ Deleted

Related PRs

Test plan

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Fixed the idle database connection cleanup process to properly remove connections when they exceed their maximum age limit. This ensures more efficient connection management across all database host groups.

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)
@coderabbitai
Copy link

coderabbitai bot commented Feb 5, 2026

📝 Walkthrough

Walkthrough

The 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

Cohort / File(s) Summary
Host Group Manager Classes
lib/Base_HostGroups_Manager.cpp, lib/MySQL_HostGroups_Manager.cpp, lib/PgSQL_HostGroups_Manager.cpp
Updated drop_all_idle_connections() to remove idle connections at loop index i instead of always removing index 0, with corresponding i-- decrement to account for shifted indices.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 Three managers hop with glee,
No more removing index zero, you see!
Now i marks the way, old connections must go,
Array indices dance in a proper-paced flow.
A small tweak that makes idle removal quite right! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: fixing an index bug in connection cleanup loops across MySQL and PgSQL managers.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/remove-wrong-index-connection-cleanup

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist
Copy link

Summary of Changes

Hello @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

  • Incorrect Connection Removal: Fixed a bug where connection cleanup loops were removing the connection at index 0 instead of the currently iterated expired connection at index i.
  • Scope of Fix: This fix applies to connection cleanup logic in both MySQL and PgSQL host group managers, specifically within the drop_all_idle_connections method.
  • Impact of Previous Bug: The bug could lead to connection pool corruption and resource leaks by deleting fresh connections and leaving expired ones in the pool, as demonstrated in the provided example scenario.

🧠 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
  • lib/Base_HostGroups_Manager.cpp
    • Corrected the index used for removing connections in drop_all_idle_connections from 0 to i.
  • lib/MySQL_HostGroups_Manager.cpp
    • Corrected the index used for removing connections in drop_all_idle_connections from 0 to i.
  • lib/PgSQL_HostGroups_Manager.cpp
    • Corrected the index used for removing connections in drop_all_idle_connections from 0 to i.
Activity
  • The author, renecannao, initiated this pull request to address a bug in connection cleanup loops.
  • The PR description clearly outlines the bug, its impact (connection pool corruption, resource leaks), and provides an example scenario.
  • The author referenced a related PR (Fix delete connection with incorrect index #5094) that fixed a similar pattern, indicating a consistent approach to resolving such issues.
  • The proposed test plan relies on manual code review and the verification of the related fix in Fix delete connection with incorrect index #5094.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Feb 5, 2026

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant