Skip to content

postgres/pgx: don't leak the conn from WithInstance when init fails - #1397

Open
c-tonneslan wants to merge 1 commit into
golang-migrate:masterfrom
c-tonneslan:fix/pgxv5-conn-leak-versiontable
Open

postgres/pgx: don't leak the conn from WithInstance when init fails#1397
c-tonneslan wants to merge 1 commit into
golang-migrate:masterfrom
c-tonneslan:fix/pgxv5-conn-leak-versiontable

Conversation

@c-tonneslan

Copy link
Copy Markdown

Fixes #1366.

`WithInstance` pulls a connection out of the `*sql.DB` pool and then runs `ensureLockTable` / `ensureVersionTable`. If either of those fails the connection was never returned to the pool, so a caller that retried (or just expected `WithInstance` failures to be recoverable) leaked one conn per attempt until the pool ran out.

`Close` the conn on every error path between `Conn()` and a successful return. Same fix applied to `database/postgres`, `database/pgx`, and `database/pgx/v5` since they all share the pattern.

WithInstance pulled a connection out of the *sql.DB pool and then
called ensureLockTable / ensureVersionTable. If either of those failed
the connection was never returned to the pool, and a caller that
retried (or just expected WithInstance failures to be benign) leaked
one conn per attempt until the pool ran out.

Close the conn on every error path between Conn() and a successful
return. Same fix applied to database/postgres, database/pgx, and
database/pgx/v5 since they all share the pattern.

Fixes golang-migrate#1366.

Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
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.

leaked conn in pgx5.WithInstance() if err return from tableversion

1 participant