libpqrcv_connect() leaks PGconn
Andres Freund <andres@anarazel.de>
From: Andres Freund <andres@anarazel.de>
To: pgsql-hackers@postgresql.org
Date: 2023-01-21T01:12:37Z
Lists: pgsql-hackers
Hi,
We have code like this in libpqrcv_connect():
conn = palloc0(sizeof(WalReceiverConn));
conn->streamConn = PQconnectStartParams(keys, vals,
/* expand_dbname = */ true);
if (PQstatus(conn->streamConn) == CONNECTION_BAD)
{
*err = pchomp(PQerrorMessage(conn->streamConn));
return NULL;
}
[try to establish connection]
if (PQstatus(conn->streamConn) != CONNECTION_OK)
{
*err = pchomp(PQerrorMessage(conn->streamConn));
return NULL;
}
Am I missing something, or are we leaking the libpq connection in case of
errors?
It doesn't matter really for walreceiver, since it will exit anyway, but we
also use libpqwalreceiver for logical replication, where it might?
Seems pretty clear that we should do a PQfinish() before returning NULL? I
lean towards thinking that this isn't worth backpatching given the current
uses of libpq, but I could easily be convinced otherwise.
Noticed while taking another look through [1].
Greetings,
Andres Freund
[1] https://www.postgresql.org/message-id/20220925232237.p6uskba2dw6fnwj2%40awork3.anarazel.de
Commits
-
Fix error handling in libpqrcv_connect()
- 243373159fb4 11.19 landed
- 92fc1278786f 12.14 landed
- c5864805ba99 13.10 landed
- 0a796b8b3e31 14.7 landed
- 704a330a9ee8 15.2 landed
- bc54ef4ec25a 16.0 landed