v2 is attached which fixes a grammatical issue in a comment.
-- Tristan Partin Neon (https://neon.tech)
From b9ccfc3c84a25b8616fd40495954bb6f77788e28 Mon Sep 17 00:00:00 2001 From: Tristan Partin <tris...@neon.tech> Date: Mon, 24 Jul 2023 11:12:59 -0500 Subject: [PATCH v2] 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..e40413a229 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, we + * can never exit from polling for the database connection. Failure to + * restore 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 overwrote + * it initially. + */ + if (rc == 0) + sigaction(SIGINT, &oldact, NULL); + pg_free(keywords); pg_free(values); -- Tristan Partin Neon (https://neon.tech)