v3 is attached which fixes up some code comments I added which I hadn't attached to the commit already, sigh.

--
Tristan Partin
Neon (https://neon.tech)
From 7f9554944911c77aa1a1900537a91e1e7bd75d93 Mon Sep 17 00:00:00 2001
From: Tristan Partin <tris...@neon.tech>
Date: Mon, 24 Jul 2023 11:12:59 -0500
Subject: [PATCH v3] Allow SIGINT to cancel psql database reconnections

After installing the SIGINT handler in psql, SIGINT can no longer cancel
database reconnections. For instance, if the user starts a reconnection
and then needs to do some form of interaction (ie psql is polling),
there is no way to cancel the reconnection process currently.

Restore the default SIGINT handler while polling in PQconnectdbParams(),
and put the custom handler back afterward.
---
 src/bin/psql/command.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 6733f008fd..11dedbb4c6 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -40,6 +40,7 @@
 #include "large_obj.h"
 #include "libpq-fe.h"
 #include "libpq/pqcomm.h"
+#include "libpq/pqsignal.h"
 #include "mainloop.h"
 #include "portability/instr_time.h"
 #include "pqexpbuffer.h"
@@ -3530,8 +3531,11 @@ do_connect(enum trivalue reuse_previous_specification,
 	{
 		const char **keywords = pg_malloc((nconnopts + 1) * sizeof(*keywords));
 		const char **values = pg_malloc((nconnopts + 1) * sizeof(*values));
+		int			rc;
 		int			paramnum = 0;
 		PQconninfoOption *ci;
+		struct sigaction oldact;
+		struct sigaction newact = { 0 };
 
 		/*
 		 * Copy non-default settings into the PQconnectdbParams parameter
@@ -3576,9 +3580,24 @@ do_connect(enum trivalue reuse_previous_specification,
 		keywords[paramnum] = NULL;
 		values[paramnum] = NULL;
 
+		/*
+		 * Restore the default SIGINT behavior while within libpq. Otherwise,
+		 * SIGINT can never exit from polling for the database connection.
+		 * Failure to restore the default is non-fatal.
+		 */
+		newact.sa_handler = SIG_DFL;
+		rc = sigaction(SIGINT, &newact, &oldact);
+
 		/* Note we do not want libpq to re-expand the dbname parameter */
 		n_conn = PQconnectdbParams(keywords, values, false);
 
+		/*
+		 * Only attempt to restore the old handler if we successfully overrode
+		 * it initially.
+		 */
+		if (rc == 0)
+			sigaction(SIGINT, &oldact, NULL);
+
 		pg_free(keywords);
 		pg_free(values);
 
-- 
Tristan Partin
Neon (https://neon.tech)

Reply via email to