Repository navigation
Conversation
Release the previous failed PGconn (from the initial attempt made by pgsql_open_connection, or from the previous retry iteration) before PQconnectdb() overwrites pgsql->connection. Fixes hapostgres#1178
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1178
Hello, and thank you for maintaining pg_auto_failover.
In
pgsql_retry_open_connection()(src/bin/common/pgsql.c), thePQPING_OKcase assigns the result ofPQconnectdb()topgsql->connectionwithout releasing the previous value. That value is either the failed first attempt made bypgsql_open_connection(), or the failed connection from the previous loop iteration, so the oldPGconnwas never freed.This patch calls
PQfinish()on the old connection and sets the pointer to NULL right before the newPQconnectdb(). The other exit paths go throughpgsql_finish(), which already sets the pointer to NULL, so I do not see a double free risk. I usedPQfinish()directly to avoid a "Disconnecting" log line on every retry.How I tested: I built pg_autoctl from main with and without the patch, and ran
pg_autoctl show state --monitor ...(with PGCONNECT_TIMEOUT=14 to allow several retries) against a small fake server that completes authentication and then stalls, so each attempt hits the connect timeout. An LD_PRELOAD counter showed 7 PGconn objects created and 1 released before the patch, and 7 created and 7 released after.What I did not test: I could not reproduce the idle backends remaining on a real monitor (libpq closed the socket at the timeout in my setup, so only the client memory leaked), and I did not run the project's test suite. Happy to adjust if you prefer a different approach.