Skip to content

Commit 9329593

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 f5e3fab commit 9329593

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
@@ -676,10 +676,6 @@ main(int argc, char **argv)
676676
close_destination_dir(dir, basedir);
677677
}
678678

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

688+
/*
689+
* Trap signals. (Don't do this until after the initial password prompt,
690+
* if one is needed, in GetConnection.)
691+
*/
692+
#ifndef WIN32
693+
pqsignal(SIGINT, sigint_handler);
694+
#endif
695+
692696
/*
693697
* Run IDENTIFY_SYSTEM to make sure we've successfully have established a
694698
* 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
@@ -214,8 +214,6 @@ StreamLogicalLog(void)
214214
output_written_lsn = InvalidXLogRecPtr;
215215
output_fsync_lsn = InvalidXLogRecPtr;
216216

217-
query = createPQExpBuffer();
218-
219217
/*
220218
* Connect in replication mode to the server
221219
*/
@@ -234,6 +232,7 @@ StreamLogicalLog(void)
234232
replication_slot);
235233

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

@@ -922,22 +921,25 @@ main(int argc, char **argv)
922921
exit(1);
923922
}
924923

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

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

0 commit comments

Comments
 (0)