Fix error handling in libpqrcv_connect()
authorAndres Freund <[email protected]>
Tue, 24 Jan 2023 02:04:02 +0000 (18:04 -0800)
committerAndres Freund <[email protected]>
Tue, 24 Jan 2023 02:27:42 +0000 (18:27 -0800)
When libpqrcv_connect (also known as walrcv_connect()) failed, it  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 .

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 <[email protected]>
Discussion: https://postgr.es/m/20230121011237[email protected]
Back: 11-

src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
src/test/regress/expected/subscription.out
src/test/regress/sql/subscription.sql

index c40c6220db8469f6a77383400bb85464f3a0b0cb..fefc8660259192cd13238c6172fa15e9985c0586 100644 (file)
@@ -175,10 +175,7 @@ libpqrcv_connect(const char *conninfo, bool logical, const char *appname,
    conn->streamConn = PQconnectStartParams(keys, vals,
                                             /* expand_dbname = */ true);
    if (PQstatus(conn->streamConn) == CONNECTION_BAD)
-   {
-       *err = pchomp(PQerrorMessage(conn->streamConn));
-       return NULL;
-   }
+       goto bad_connection_errmsg;
 
    /*
     * Poll connection until we have OK or FAILED status.
@@ -220,10 +217,7 @@ libpqrcv_connect(const char *conninfo, bool logical, const char *appname,
    } while (status != PGRES_POLLING_OK && status != PGRES_POLLING_FAILED);
 
    if (PQstatus(conn->streamConn) != CONNECTION_OK)
-   {
-       *err = pchomp(PQerrorMessage(conn->streamConn));
-       return NULL;
-   }
+       goto bad_connection_errmsg;
 
    if (logical)
    {
@@ -234,9 +228,9 @@ libpqrcv_connect(const char *conninfo, bool logical, const char *appname,
        if (PQresultStatus(res) != PGRES_TUPLES_OK)
        {
            PQclear(res);
-           ereport(ERROR,
-                   (errmsg("could not clear search path: %s",
-                           pchomp(PQerrorMessage(conn->streamConn)))));
+           *err = psprintf(_("could not clear search path: %s"),
+                           pchomp(PQerrorMessage(conn->streamConn)));
+           goto bad_connection;
        }
        PQclear(res);
    }
@@ -244,6 +238,16 @@ libpqrcv_connect(const char *conninfo, bool logical, const char *appname,
    conn->logical = logical;
 
    return conn;
+
+   /* error path, using libpq's error message */
+bad_connection_errmsg:
+   *err = pchomp(PQerrorMessage(conn->streamConn));
+
+   /* error path, error already set */
+bad_connection:
+   PQfinish(conn->streamConn);
+   pfree(conn);
+   return NULL;
 }
 
 /*
index 4e5cb0d3a971329a31b1e5d66a53fa6235a25fe1..3f99b143945dc41f44a709caaf31ea4429aa3f47 100644 (file)
@@ -130,7 +130,15 @@ ALTER SUBSCRIPTION regress_testsub4 SET (origin = any);
 
 DROP SUBSCRIPTION regress_testsub3;
 DROP SUBSCRIPTION regress_testsub4;
--- fail - invalid connection string
+-- fail, connection string does not parse
+CREATE SUBSCRIPTION regress_testsub5 CONNECTION 'i_dont_exist=param' PUBLICATION testpub;
+ERROR:  invalid connection string syntax: invalid connection option "i_dont_exist"
+
+-- fail, connection string parses, but doesn't work (and does so without
+-- connecting, so this is reliable and safe)
+CREATE SUBSCRIPTION regress_testsub5 CONNECTION 'port=-1' PUBLICATION testpub;
+ERROR:  could not connect to the publisher: invalid port number: "-1"
+-- fail - invalid connection string during ALTER
 ALTER SUBSCRIPTION regress_testsub CONNECTION 'foobar';
 ERROR:  invalid connection string syntax: missing "=" after "foobar" in connection info string
 
index 5f27b7d7763869f5e67f07099e51943fedcd8fe3..7281f5fee2ba13ac84b7a9e4c9c297b7de4ee9a3 100644 (file)
@@ -77,7 +77,14 @@ ALTER SUBSCRIPTION regress_testsub4 SET (origin = any);
 DROP SUBSCRIPTION regress_testsub3;
 DROP SUBSCRIPTION regress_testsub4;
 
--- fail - invalid connection string
+-- fail, connection string does not parse
+CREATE SUBSCRIPTION regress_testsub5 CONNECTION 'i_dont_exist=param' PUBLICATION testpub;
+
+-- fail, connection string parses, but doesn't work (and does so without
+-- connecting, so this is reliable and safe)
+CREATE SUBSCRIPTION regress_testsub5 CONNECTION 'port=-1' PUBLICATION testpub;
+
+-- fail - invalid connection string during ALTER
 ALTER SUBSCRIPTION regress_testsub CONNECTION 'foobar';
 
 \dRs+