When attempting to connect to a non-existent host with psql, the
connection will hang and ^C will not break the attempt. This affects two
places: startup and in interactive mode with \c. During startup, the new
behavior will be to exit psql. In interactive mode, the new behavior
will be to return to the interactive shell.
---
 src/bin/psql/command.c |   17 +++++++++++++----
 src/bin/psql/startup.c |    5 ++---
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 69fac83..74e406b 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -1516,7 +1516,7 @@ static bool
 do_connect(char *dbname, char *user, char *host, char *port)
 {
        PGconn     *o_conn = pset.db,
-                          *n_conn;
+                          *n_conn = NULL;
        char       *password = NULL;
 
        if (!dbname)
@@ -1570,7 +1570,13 @@ do_connect(char *dbname, char *user, char *host, char 
*port)
                keywords[7] = NULL;
                values[7] = NULL;
 
-               n_conn = PQconnectdbParams(keywords, values, true);
+               if (sigsetjmp(sigint_interrupt_jmp, 1) != 0) {
+                       /* got here with longjmp */
+               } else {
+                       sigint_interrupt_enabled = true;
+                       n_conn = PQconnectdbParams(keywords, values, true);
+                       sigint_interrupt_enabled = false;
+               }
 
                free(keywords);
                free(values);
@@ -1600,7 +1606,8 @@ do_connect(char *dbname, char *user, char *host, char 
*port)
                 */
                if (pset.cur_cmd_interactive)
                {
-                       psql_error("%s", PQerrorMessage(n_conn));
+                       if (n_conn)
+                               psql_error("%s", PQerrorMessage(n_conn));
 
                        /* pset.db is left unmodified */
                        if (o_conn)
@@ -1608,7 +1615,9 @@ do_connect(char *dbname, char *user, char *host, char 
*port)
                }
                else
                {
-                       psql_error("\\connect: %s", PQerrorMessage(n_conn));
+                       if (n_conn)
+                               psql_error("\\connect: %s", 
PQerrorMessage(n_conn));
+
                        if (o_conn)
                        {
                                PQfinish(o_conn);
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 8b1864c..e53d84c 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -111,8 +111,6 @@ main(int argc, char *argv[])
        setvbuf(stderr, NULL, _IONBF, 0);
 #endif
 
-       setup_cancel_handler();
-
        pset.progname = get_progname(argv[0]);
 
        pset.db = NULL;
@@ -245,8 +243,9 @@ main(int argc, char *argv[])
        }
 
        /*
-        * Now find something to do
+        * Now find something to do (and handle cancellation, if applicable)
         */
+       setup_cancel_handler();
 
        /*
         * process file given by -f
-- 
1.7.7.4

Previously, these changes were submitted to -bugs:
http://archives.postgresql.org/pgsql-bugs/2012-01/msg00030.php
http://archives.postgresql.org/pgsql-bugs/2012-01/msg00036.php

Though, I think that might not have been the correct forum? I still could be
wrong with -hackers, so just let me know where to go with this if I'm still
off-base.

-Ryan Kelly


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to