Skip to content

Commit 33edf4a

Browse files
committed
pg_receivewal, pg_recvlogical: allow canceling initial password prompt.
Previously it was impossible to terminate these programs via control-C while they were prompting for a password. We can fix that trivially for their initial password prompts, by moving setup of the SIGINT handler from just before to just after their initial GetConnection() calls. This fix doesn't permit escaping out of later re-prompts, but those should be exceedingly rare, since the user's password or the server's authentication setup would have to have changed meanwhile. We considered applying a fix similar to commit 46d665b, but that seemed more complicated than it'd be worth. Moreover, this way is back-patchable, which that wasn't. The misbehavior exists in all supported versions, so back-patch to all. Tom Lane and Nathan Bossart Discussion: https://postgr.es/m/747443.1635536754@sss.pgh.pa.us
1 parent 33b6dd8 commit 33edf4a

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

src/bin/pg_basebackup/pg_receivewal.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -675,10 +675,6 @@ main(int argc, char **argv)
675675
close_destination_dir(dir, basedir);
676676
}
677677

678-
#ifndef WIN32
679-
pqsignal(SIGINT, sigint_handler);
680-
#endif
681-
682678
/*
683679
* Obtain a connection before doing anything.
684680
*/
@@ -688,6 +684,14 @@ main(int argc, char **argv)
688684
exit(1);
689685
atexit(disconnect_atexit);
690686

687+
/*
688+
* Trap signals. (Don't do this until after the initial password prompt,
689+
* if one is needed, in GetConnection.)
690+
*/
691+
#ifndef WIN32
692+
pqsignal(SIGINT, sigint_handler);
693+
#endif
694+
691695
/*
692696
* Run IDENTIFY_SYSTEM to make sure we've successfully have established a
693697
* replication connection and haven't connected using a database specific

src/bin/pg_basebackup/pg_recvlogical.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,6 @@ StreamLogicalLog(void)
212212
output_written_lsn = InvalidXLogRecPtr;
213213
output_fsync_lsn = InvalidXLogRecPtr;
214214

215-
query = createPQExpBuffer();
216-
217215
/*
218216
* Connect in replication mode to the server
219217
*/
@@ -232,6 +230,7 @@ StreamLogicalLog(void)
232230
replication_slot);
233231

234232
/* Initiate the replication stream at specified location */
233+
query = createPQExpBuffer();
235234
appendPQExpBuffer(query, "START_REPLICATION SLOT \"%s\" LOGICAL %X/%X",
236235
replication_slot, (uint32) (startpos >> 32), (uint32) startpos);
237236

@@ -920,22 +919,25 @@ main(int argc, char **argv)
920919
exit(1);
921920
}
922921

923-
#ifndef WIN32
924-
pqsignal(SIGINT, sigint_handler);
925-
pqsignal(SIGHUP, sighup_handler);
926-
#endif
927-
928922
/*
929-
* Obtain a connection to server. This is not really necessary but it
930-
* helps to get more precise error messages about authentication, required
931-
* GUC parameters and such.
923+
* Obtain a connection to server. Notably, if we need a password, we want
924+
* to collect it from the user immediately.
932925
*/
933926
conn = GetConnection();
934927
if (!conn)
935928
/* Error message already written in GetConnection() */
936929
exit(1);
937930
atexit(disconnect_atexit);
938931

932+
/*
933+
* Trap signals. (Don't do this until after the initial password prompt,
934+
* if one is needed, in GetConnection.)
935+
*/
936+
#ifndef WIN32
937+
pqsignal(SIGINT, sigint_handler);
938+
pqsignal(SIGHUP, sighup_handler);
939+
#endif
940+
939941
/*
940942
* Run IDENTIFY_SYSTEM to make sure we connected using a database specific
941943
* replication connection.

0 commit comments

Comments
 (0)