From: Tristan Partin <tristan@neon.tech>
To: Tristan Partin <tristan@neon.tech>
To: pgsql-hackers@postgresql.org
Subject: Re: psql not responding to SIGINT upon db reconnection
Date: Mon, 24 Jul 2023 12:11:27 -0500
Message-ID: <CUAKIOVISECO.2S7MSQFOP1304@gonk> (raw)
In-Reply-To: <CUAKHCX5472I.SJP0AWQRYVFA@gonk>
References: <CUAJJDVD8L61.2AH6VEEDMTXX3@gonk>
<CUAKHCX5472I.SJP0AWQRYVFA@gonk>
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)
Attachments:v3-0001-Allow-SIGINT-to-cancel-psql-database-reconnection.patch (text/x-patch, 2.2K) download
From 7f9554944911c77aa1a1900537a91e1e7bd75d93 Mon Sep 17 00:00:00 2001
From: Tristan Partin <tristan@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.cindex 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 instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-hackers@postgresql.org
Cc: tristan@neon.tech
Subject: Re: psql not responding to SIGINT upon db reconnection
In-Reply-To: <CUAKIOVISECO.2S7MSQFOP1304@gonk>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox