diff options
author | Michael Paquier <michael@paquier.xyz> | 2019-12-17 10:44:25 +0900 |
---|---|---|
committer | Michael Paquier <michael@paquier.xyz> | 2019-12-17 10:44:25 +0900 |
commit | 5d43c3c54d77f39135fe463539f5f438f460ae7e (patch) | |
tree | 2f678bc823dcc865a64f382a6e8d892d96b69220 /src/bin/psql/common.c | |
parent | b925a00f4ef65db9359e1c60fbf0e56d05afb25a (diff) |
Fix query cancellation handling in psql
The refactoring done in a4fd3aa for query cancellation has messed up
with the logic in psql by mixing CancelRequested and cancel_pressed,
breaking for example \watch. The former would be switched to true if a
cancellation request has been attempted and that it actually succeeded,
and the latter tracks if a cancellation attempt has been done.
This commit brings back the code of psql to a state consistent to what
it was before a4fd3aa, without giving up on the refactoring pieces
introduced. It should be actually possible to merge more both flags as
their concepts are close enough, however note that psql's --single-step
mode relies on cancel_pressed to be always set, so this requires more
careful analysis left for later.
While on it, fix the declarations of CancelRequested (in cancel.c) and
cancel_pressed (in psql) to be volatile sig_atomic_t. Previously,
both were declared as booleans, which should be fine on modern
platforms, but the C standard recommends the use of sig_atomic_t for
variables used in signal handlers. Note that since its introduction in
a1792320, CancelRequested declaration was not volatile.
Reported-by: Jeff Janes
Author: Michael Paquier
Discussion: https://postgr.es/m/CAMkU=1zpoUDGKqWKuMWkj7t-bOCaJDx0r=5te_-d0B2HVLABXg@mail.gmail.com
Diffstat (limited to 'src/bin/psql/common.c')
-rw-r--r-- | src/bin/psql/common.c | 28 |
1 files changed, 9 insertions, 19 deletions
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 9c0d53689ec..9aef756f25c 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -233,7 +233,7 @@ NoticeProcessor(void *arg, const char *message) * * SIGINT is supposed to abort all long-running psql operations, not only * database queries. In most places, this is accomplished by checking - * CancelRequested during long-running loops. However, that won't work when + * cancel_pressed during long-running loops. However, that won't work when * blocked on user input (in readline() or fgets()). In those places, we * set sigint_interrupt_enabled true while blocked, instructing the signal * catcher to longjmp through sigint_interrupt_jmp. We assume readline and @@ -246,32 +246,22 @@ volatile bool sigint_interrupt_enabled = false; sigjmp_buf sigint_interrupt_jmp; -#ifndef WIN32 - static void psql_cancel_callback(void) { +#ifndef WIN32 /* if we are waiting for input, longjmp out of it */ if (sigint_interrupt_enabled) { sigint_interrupt_enabled = false; siglongjmp(sigint_interrupt_jmp, 1); } +#endif /* else, set cancel flag to stop any long-running loops */ - CancelRequested = true; -} - -#else - -static void -psql_cancel_callback(void) -{ - /* nothing to do here */ + cancel_pressed = true; } -#endif /* !WIN32 */ - void psql_setup_cancel_handler(void) { @@ -638,7 +628,7 @@ PSQLexecWatch(const char *query, const printQueryOpt *opt) * consumed. The user's intention, though, is to cancel the entire watch * process, so detect a sent cancellation request and exit in this case. */ - if (CancelRequested) + if (cancel_pressed) { PQclear(res); return 0; @@ -838,8 +828,8 @@ ExecQueryTuples(const PGresult *result) { const char *query = PQgetvalue(result, r, c); - /* Abandon execution if CancelRequested */ - if (CancelRequested) + /* Abandon execution if cancel_pressed */ + if (cancel_pressed) goto loop_exit; /* @@ -1207,7 +1197,7 @@ SendQuery(const char *query) if (fgets(buf, sizeof(buf), stdin) != NULL) if (buf[0] == 'x') goto sendquery_cleanup; - if (CancelRequested) + if (cancel_pressed) goto sendquery_cleanup; } else if (pset.echo == PSQL_ECHO_QUERIES) @@ -1751,7 +1741,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) * writing things to the stream, we presume $PAGER has disappeared and * stop bothering to pull down more data. */ - if (ntuples < fetch_count || CancelRequested || flush_error || + if (ntuples < fetch_count || cancel_pressed || flush_error || ferror(fout)) break; } |