psql: Stop scripts on Ctrl+C while waiting for COPY or prompt input

When running a script non-interactively, pressing Ctrl+C while a query
is running aborts the whole script and psql exits with code 3. But when
psql was instead waiting for input, e.g. COPY FROM STDIN data via
\copy ... from pstdin, or input for \prompt or \password, pressing
Ctrl+C only aborted that single command and the rest of the script
continued running. For example with this script:

    \copy t1 from pstdin
    select now();

Pressing Ctrl+C while psql is waiting for COPY data would abort the
COPY, but then still run the SELECT and exit successfully.

The reason for this inconsistency was that psql_cancel_callback() only
set cancel_pressed when it did not longjmp out of waiting for input.
That's fine for the longjmp target in MainLoop(), which itself already
stops a non-interactive script when psql gets interrupted while waiting
for the next line of input. But the longjmp targets in handleCopyIn()
and pg_get_line_append() (used by simple_prompt_extended()) simply
return to their callers, which then have no way of knowing that the
user wanted to stop the script.

This fixes that by always setting cancel_pressed before doing the
longjmp. Interactive sessions are not affected, because MainLoop()
clears cancel_pressed again for those.

Author: Jelte Fennema-Nio <[email protected]>
Discussion: 
https://www.postgresql.org/message-id/flat/[email protected]

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/8b529705d153f6bc592f874a9f2dd47be8fa0589

Modified Files
--------------
src/bin/psql/common.c | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)

Reply via email to