From 77d21970ae19418f321e6a76ddf1a57ae999c77a Mon Sep 17 00:00:00 2001 From: Fujii Masao Date: Wed, 8 Mar 2017 23:43:38 +0900 Subject: [PATCH] Fix connection leak in DROP SUBSCRIPTION command, take 2. Commit 898a792eb8283e31efc0b6fcbc03bbcd5f7df667 fixed the connection leak issue, but it was an unreliable way of bugfix. This bugfix was assuming that walrcv_command() subroutine cannot throw an error, but it's untenable assumption. For example, if it will be changed so that an error is thrown, connection leak issue will happen again. This patch ensures that the connection is closed even when walrcv_command() subroutine throws an error. Patch by me, reviewed by Petr Jelinek and Michael Paquier Discussion: https://www.postgresql.org/message-id/2058.1487704345@sss.pgh.pa.us --- src/backend/commands/subscriptioncmds.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c index 0036d99c2e..247efc8e6e 100644 --- a/src/backend/commands/subscriptioncmds.c +++ b/src/backend/commands/subscriptioncmds.c @@ -565,19 +565,25 @@ DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel) "drop the replication slot \"%s\"", slotname), errdetail("The error was: %s", err))); - if (!walrcv_command(wrconn, cmd.data, &err)) + PG_TRY(); + { + if (!walrcv_command(wrconn, cmd.data, &err)) + ereport(ERROR, + (errmsg("could not drop the replication slot \"%s\" on publisher", + slotname), + errdetail("The error was: %s", err))); + else + ereport(NOTICE, + (errmsg("dropped replication slot \"%s\" on publisher", + slotname))); + } + PG_CATCH(); { /* Close the connection in case of failure */ walrcv_disconnect(wrconn); - ereport(ERROR, - (errmsg("could not drop the replication slot \"%s\" on publisher", - slotname), - errdetail("The error was: %s", err))); + PG_RE_THROW(); } - else - ereport(NOTICE, - (errmsg("dropped replication slot \"%s\" on publisher", - slotname))); + PG_END_TRY(); walrcv_disconnect(wrconn);