Fix error handling in libpqrcv_connect()
authorAndres Freund
Tue, 24 Jan 2023 02:04:02 +0000 (18:04 -0800)
committerAndres Freund
Tue, 24 Jan 2023 02:27:48 +0000 (18:27 -0800)
commit0a796b8b3e311ea21cf679bf71f39b42e859a686
tree08ae387ec04595bbbca374472ea691d5113caf2a
parent0765b2f8f6fe8b7c1c07f94b764ef8bb74a292b4
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
Discussion: https://postgr.es/m/20230121011237[email protected]
Backpatch: 11-
src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
src/test/regress/expected/subscription.out
src/test/regress/sql/subscription.sql