Neon provides a quick start mechanism for psql using the following
workflow:
$ psql -h pg.neon.tech
NOTICE: Welcome to Neon!
Authenticate by visiting:
https://console.neon.tech/psql_session/xxx
Upon navigating to the link, the user selects their database to work
with. The psql process is then connected to a Postgres database at Neon.
psql provides a way to reconnect to the database from within itself: \c.
If, after connecting to a database such as the process previously
mentioned, the user starts a reconnection to the database from within
psql, the process will refuse to interrupt the reconnection attempt
after sending SIGINT to it.
tristan=> \c
NOTICE: Welcome to Neon!
Authenticate by visiting:
https://console.neon.tech/psql_session/yyy
^C
^C^C
^C^C
I am not really sure if this was a problem on Windows machines, but the
attached patch should support it if it does. If this was never a problem
on Windows, it might be best to check for any regressions.
This was originally discussed in the a GitHub issue[0] at Neon.
[0]: https://github.com/neondatabase/neon/issues/1449
--
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 v1] 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)