8000 Fix error handling in libpqrcv_connect() · postgres/postgres@2433731 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2433731

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 6c122ed commit 2433731

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
@@ -161,10 +161,7 @@ libpqrcv_connect(const char *conninfo, bool logical, const char *appname,
161161
conn->streamConn = PQconnectStartParams(keys, vals,
162162
/* expand_dbname = */ true);
163163
if (PQstatus(conn->streamConn) == CONNECTION_BAD)
164-
{
165-
*err = pchomp(PQerrorMessage(conn->streamConn));
166-
return NULL;
167-
}
164+
goto bad_connection_errmsg;
168165

169166
/*
170167
* Poll connection until we have OK or FAILED status.
@@ -211,10 +208,7 @@ libpqrcv_connect(const char *conninfo, bool logical, const char *appname,
211208
} while (status != PGRES_POLLING_OK && status != PGRES_POLLING_FAILED);
212209

213210
if (PQstatus(conn->streamConn) != CONNECTION_OK)
214-
{
215-
*err = pchomp(PQerrorMessage(conn->streamConn));
216-
return NULL;
217-
}
211+
goto bad_connection_errmsg;
218212

219213
if (logical)
220214
{
@@ -225,16 +219,26 @@ libpqrcv_connect(const char *conninfo, bool logical, const char *appname,
225219
if (PQresultStatus(res) != PGRES_TUPLES_OK)
226220
{
227221
PQclear(res);
228-
ereport(ERROR,
229-
(errmsg("could not clear search path: %s",
230-
pchomp(PQerrorMessage(conn->streamConn)))));
222+
*err = psprintf(_("could not clear search path: %s"),
223+
pchomp(PQerrorMessage(conn->streamConn)));
224+
goto bad_connection;
231225
}
232226
PQclear(res);
233227
}
234228

235229
conn->logical = logical;
236230

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

240244
/*

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 testsub3 REFRESH PUBLICATION;
7272
ERROR: ALTER SUBSCRIPTION ... REFRESH is not allowed for disabled subscriptions
7373
DROP SUBSCRIPTION 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 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 testsub3 REFRESH PUBLICATION;
5656

5757
DROP SUBSCRIPTION 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 testsub CONNECTION 'foobar';
6168

6269
\dRs+

0 commit comments

Comments
 (0)
0