Skip to content

Close Postgres connections on both success and failure paths (#104)#105

Open
BWilliams1004 wants to merge 1 commit into
mainfrom
bwilliams/104-connection-leaks
Open

Close Postgres connections on both success and failure paths (#104)#105
BWilliams1004 wants to merge 1 commit into
mainfrom
bwilliams/104-connection-leaks

Conversation

@BWilliams1004

Copy link
Copy Markdown
Contributor

Closes #104. Two connection leaks fixed:

sql2csv.py's run_sql_script() only called connection.close() on the
success path if the query raised, execution jumped to the except block, and the connection was never closed. Now wrapped in a finally block.
(with a None guard, since psycopg2.connect() itself could fail before
connection is ever assigned, so it always closes, success or failure.

run_pg.py's run_pg() never closed its connection at all. Rewritten to
Use with context managers for both the connection and the cursor, which
close automatically on exit regardless of outcome.

Testing: Verified locally end-to-end for both. Set up an isolated venv for
sql2datasets its dependencies conflicted with the main venv’s pinned
aiobotocore/botocore versions used a separate env-sql2datasets to
avoid that going forward. For each fix, I ran both a real failing query and
a real successful query against the live database, then confirmed via
pg_stat_activity, the connection counts stayed flat afterward in both
cases, no leaked sessions on success or failure.

@BWilliams1004
BWilliams1004 requested a review from gankoji July 17, 2026 07:22
@BWilliams1004 BWilliams1004 self-assigned this Jul 17, 2026
@BWilliams1004 BWilliams1004 added the bug Something isn't working label Jul 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Postgres connections leak on query failure (sql2csv.py) and always (run_pg.py)

1 participant