Skip to content

Commit 704a330

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 5dc582d commit 704a330

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
@@ -177,10 +177,7 @@ libpqrcv_connect(const char *conninfo, bool logical, const char *appname,
177177
conn->streamConn = PQconnectStartParams(keys, vals,
178178
/* expand_dbname = */ true);
179179
if (PQstatus(conn->streamConn) == CONNECTION_BAD)
180-
{
181-
*err = pchomp(PQerrorMessage(conn->streamConn));
182-
return NULL;
183-
}
180+
goto bad_connection_errmsg;
184181

185182
/*
186183
* Poll connection until we have OK or FAILED status.
@@ -222,10 +219,7 @@ libpqrcv_connect(const char *conninfo, bool logical, const char *appname,
222219
} while (status != PGRES_POLLING_OK && status != PGRES_POLLING_FAILED);
223220

224221
if (PQstatus(conn->streamConn) != CONNECTION_OK)
225-
{
226-
*err = pchomp(PQerrorMessage(conn->streamConn));
227-
return NULL;
228-
}
222+
goto bad_connection_errmsg;
229223

230224
if (logical)
231225
{
@@ -236,16 +230,26 @@ libpqrcv_connect(const char *conninfo, bool logical, const char *appname,
236230
if (PQresultStatus(res) != PGRES_TUPLES_OK)
237231
{
238232
PQclear(res);
239-
ereport(ERROR,
240-
(errmsg("could not clear search path: %s",
241-
pchomp(PQerrorMessage(conn->streamConn)))));
233+
*err = psprintf(_("could not clear search path: %s"),
234+
pchomp(PQerrorMessage(conn->streamConn)));
235+
goto bad_connection;
242236
}
243237
PQclear(res);
244238
}
245239

246240
conn->logical = logical;
247241

248242
return conn;
243+
244+
/* error path, using libpq's error message */
245+
bad_connection_errmsg:
246+
*err = pchomp(PQerrorMessage(conn->streamConn));
247+
248+
/* error path, error already set */
249+
bad_connection:
250+
PQfinish(conn->streamConn);
251+
pfree(conn);
252+
return NULL;
249253
}
250254

251255
/*

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)