Skip to content

Commit 0a796b8

Browse files
committed
Fix error handling in libpqrcv_connect()
When libpqrcv_connect (also known as walrcv_connect()) failed, it leaked the libpq connection. In most paths that's fairly harmless, as the calling process will exit soon after. But e.g. CREATE SUBSCRIPTION could lead to a somewhat longer lived leak. Fix by releasing resources, including the libpq connection, on error. Add a test exercising the error code path. To make it reliable and safe, the test tries to connect to port=-1, which happens to fail during connection establishment, rather than during connection string parsing. Reviewed-by: Noah Misch <noah@leadboat.com> Discussion: https://postgr.es/m/20230121011237.q52apbvlarfv6jm6@awork3.anarazel.de Backpatch: 11-
1 parent 0765b2f commit 0a796b8

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

src/backend/replication/libpqwalreceiver/libpqwalreceiver.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,7 @@ libpqrcv_connect(const char *conninfo, bool logical, const char *appname,
164164
conn->streamConn = PQconnectStartParams(keys, vals,
165165
/* expand_dbname = */ true);
166166
if (PQstatus(conn->streamConn) == CONNECTION_BAD)
167-
{
168-
*err = pchomp(PQerrorMessage(conn->streamConn));
169-
return NULL;
170-
}
167+
goto bad_connection_errmsg;
171168

172169
/*
173170
* Poll connection until we have OK or FAILED status.
@@ -209,10 +206,7 @@ libpqrcv_connect(const char *conninfo, bool logical, const char *appname,
209206
} while (status != PGRES_POLLING_OK && status != PGRES_POLLING_FAILED);
210207

211208
if (PQstatus(conn->streamConn) != CONNECTION_OK)
212-
{
213-
*err = pchomp(PQerrorMessage(conn->streamConn));
214-
return NULL;
215-
}
209+
goto bad_connection_errmsg;
216210

217211
if (logical)
218212
{
@@ -223,16 +217,26 @@ libpqrcv_connect(const char *conninfo, bool logical, const char *appname,
223217
if (PQresultStatus(res) != PGRES_TUPLES_OK)
224218
{
225219
PQclear(res);
226-
ereport(ERROR,
227-
(errmsg("could not clear search path: %s",
228-
pchomp(PQerrorMessage(conn->streamConn)))));
220+
*err = psprintf(_("could not clear search path: %s"),
221+
pchomp(PQerrorMessage(conn->streamConn)));
222+
goto bad_connection;
229223
}
230224
PQclear(res);
231225
}
232226

233227
conn->logical = logical;
234228

235229
return conn;
230+
231+
/* error path, using libpq's error message */
232+
bad_connection_errmsg:
233+
*err = pchomp(PQerrorMessage(conn->streamConn));
234+
235+
/* error path, error already set */
236+
bad_connection:
237+
PQfinish(conn->streamConn);
238+
pfree(conn);
239+
return NULL;
236240
}
237241

238242
/*

src/test/regress/expected/subscription.out

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,15 @@ ERROR: cannot enable subscription that does not have a slot name
7171
ALTER SUBSCRIPTION regress_testsub3 REFRESH PUBLICATION;
7272
ERROR: ALTER SUBSCRIPTION ... REFRESH is not allowed for disabled subscriptions
7373
DROP SUBSCRIPTION regress_testsub3;
74-
-- fail - invalid connection string
74+
-- fail, connection string does not parse
75+
CREATE SUBSCRIPTION regress_testsub5 CONNECTION 'i_dont_exist=param' PUBLICATION testpub;
76+
ERROR: invalid connection string syntax: invalid connection option "i_dont_exist"
77+
78+
-- fail, connection string parses, but doesn't work (and does so without
79+
-- connecting, so this is reliable and safe)
80+
CREATE SUBSCRIPTION regress_testsub5 CONNECTION 'port=-1' PUBLICATION testpub;
81+
ERROR: could not connect to the publisher: invalid port number: "-1"
82+
-- fail - invalid connection string during ALTER
7583
ALTER SUBSCRIPTION regress_testsub CONNECTION 'foobar';
7684
ERROR: invalid connection string syntax: missing "=" after "foobar" in connection info string
7785

src/test/regress/sql/subscription.sql

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,14 @@ ALTER SUBSCRIPTION regress_testsub3 REFRESH PUBLICATION;
5656

5757
DROP SUBSCRIPTION regress_testsub3;
5858

59-
-- fail - invalid connection string
59+
-- fail, connection string does not parse
60+
CREATE SUBSCRIPTION regress_testsub5 CONNECTION 'i_dont_exist=param' PUBLICATION testpub;
61+
62+
-- fail, connection string parses, but doesn't work (and does so without
63+
-- connecting, so this is reliable and safe)
64+
CREATE SUBSCRIPTION regress_testsub5 CONNECTION 'port=-1' PUBLICATION testpub;
65+
66+
-- fail - invalid connection string during ALTER
6067
ALTER SUBSCRIPTION regress_testsub CONNECTION 'foobar';
6168

6269
\dRs+

0 commit comments

Comments
 (0)