Skip to content

Commit 2437111

Browse files
gkokolatosGeorgios Kokolatos
andauthored
Do not leak psycopg2 connections during testing (#628)
According to psycopg2 documentation connections are not closed on context exit. This is in contrast with connection cursors that actually do so. It is not an inherent problem in itself, however, calls to get_state() in a loop, such as from wait_until_*, can overflow the connection limits and cause the test suite to fail. This is particularly noticable when the system is slow to respond, for example if it is run under valgrind. This might address other objerved flakyness in the tests due to connection exhaustion with messages as: "psycopg2.OperationalError: FATAL: sorry, too many clients already" Co-authored-by: Georgios Kokolatos <gkokolatos@pm.com>
1 parent cc9be24 commit 2437111

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

tests/pgautofailover_utils.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -359,14 +359,21 @@ def run_sql_query(self, query, *args):
359359
Runs the given sql query with the given arguments in this postgres node
360360
and returns the results. Returns None if there are no results to fetch.
361361
"""
362-
with psycopg2.connect(self.connection_string()) as conn:
363-
cur = conn.cursor()
364-
cur.execute(query, args)
365-
try:
366-
result = cur.fetchall()
367-
return result
368-
except psycopg2.ProgrammingError:
369-
return None
362+
result = None
363+
conn = psycopg2.connect(self.connection_string())
364+
365+
with conn:
366+
with conn.cursor() as cur:
367+
cur.execute(query, args)
368+
try:
369+
result = cur.fetchall()
370+
except psycopg2.ProgrammingError:
371+
pass
372+
# leaving contexts closes the cursor, however
373+
# leaving contexts doesn't close the connection
374+
conn.close()
375+
376+
return result
370377

371378
def pg_config_get(self, settings):
372379
"""

0 commit comments

Comments
 (0)