Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit92fc127

Browse files
committed
Fix error handling in libpqrcv_connect()
When libpqrcv_connect (also known as walrcv_connect()) failed, it leaked thelibpq connection. In most paths that's fairly harmless, as the calling processwill exit soon after. But e.g. CREATE SUBSCRIPTION could lead to a somewhatlonger 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, thetest tries to connect to port=-1, which happens to fail during connectionestablishment, rather than during connection string parsing.Reviewed-by: Noah Misch <noah@leadboat.com>Discussion:https://postgr.es/m/20230121011237.q52apbvlarfv6jm6@awork3.anarazel.deBackpatch: 11-
1 parenta5f3f2f commit92fc127

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
@@ -162,10 +162,7 @@ libpqrcv_connect(const char *conninfo, bool logical, const char *appname,
162162
conn->streamConn=PQconnectStartParams(keys,vals,
163163
/* expand_dbname = */ true);
164164
if (PQstatus(conn->streamConn)==CONNECTION_BAD)
165-
{
166-
*err=pchomp(PQerrorMessage(conn->streamConn));
167-
returnNULL;
168-
}
165+
gotobad_connection_errmsg;
169166

170167
/*
171168
* Poll connection until we have OK or FAILED status.
@@ -207,10 +204,7 @@ libpqrcv_connect(const char *conninfo, bool logical, const char *appname,
207204
}while (status!=PGRES_POLLING_OK&&status!=PGRES_POLLING_FAILED);
208205

209206
if (PQstatus(conn->streamConn)!=CONNECTION_OK)
210-
{
211-
*err=pchomp(PQerrorMessage(conn->streamConn));
212-
returnNULL;
213-
}
207+
gotobad_connection_errmsg;
214208

215209
if (logical)
216210
{
@@ -221,16 +215,26 @@ libpqrcv_connect(const char *conninfo, bool logical, const char *appname,
221215
if (PQresultStatus(res)!=PGRES_TUPLES_OK)
222216
{
223217
PQclear(res);
224-
ereport(ERROR,
225-
(errmsg("could not clear search path: %s",
226-
pchomp(PQerrorMessage(conn->streamConn)))));
218+
*err=psprintf(_("could not clear search path: %s"),
219+
pchomp(PQerrorMessage(conn->streamConn)));
220+
gotobad_connection;
227221
}
228222
PQclear(res);
229223
}
230224

231225
conn->logical=logical;
232226

233227
returnconn;
228+
229+
/* error path, using libpq's error message */
230+
bad_connection_errmsg:
231+
*err=pchomp(PQerrorMessage(conn->streamConn));
232+
233+
/* error path, error already set */
234+
bad_connection:
235+
PQfinish(conn->streamConn);
236+
pfree(conn);
237+
returnNULL;
234238
}
235239

236240
/*

‎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)

[8]ページ先頭

©2009-2025 Movatter.jp