diff options
| author | Michael Paquier | 2019-12-17 01:44:25 +0000 |
|---|---|---|
| committer | Michael Paquier | 2019-12-17 01:44:25 +0000 |
| commit | 5d43c3c54d77f39135fe463539f5f438f460ae7e (patch) | |
| tree | 2f678bc823dcc865a64f382a6e8d892d96b69220 /src/fe_utils | |
| 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/fe_utils')
| -rw-r--r-- | src/fe_utils/cancel.c | 15 | ||||
| -rw-r--r-- | src/fe_utils/print.c | 3 |
2 files changed, 14 insertions, 4 deletions
diff --git a/src/fe_utils/cancel.c b/src/fe_utils/cancel.c index 04e0d1e3b2d..9c3922b5c40 100644 --- a/src/fe_utils/cancel.c +++ b/src/fe_utils/cancel.c @@ -16,7 +16,6 @@ #include "postgres_fe.h" -#include <signal.h> #include <unistd.h> #include "fe_utils/cancel.h" @@ -37,8 +36,20 @@ (void) rc_; \ } while (0) +/* + * Contains all the information needed to cancel a query issued from + * a database connection to the backend. + */ static PGcancel *volatile cancelConn = NULL; -bool CancelRequested = false; + +/* + * CancelRequested tracks if a cancellation request has completed after + * a signal interruption. Note that if cancelConn is not set, in short + * if SetCancelConn() was never called or if ResetCancelConn() freed + * the cancellation object, then CancelRequested is switched to true after + * all cancellation attempts. + */ +volatile sig_atomic_t CancelRequested = false; #ifdef WIN32 static CRITICAL_SECTION cancelConnLock; diff --git a/src/fe_utils/print.c b/src/fe_utils/print.c index b9cd6a17521..bcc77f471cf 100644 --- a/src/fe_utils/print.c +++ b/src/fe_utils/print.c @@ -19,7 +19,6 @@ #include <limits.h> #include <math.h> -#include <signal.h> #include <unistd.h> #ifndef WIN32 @@ -41,7 +40,7 @@ * Note: print.c's general strategy for when to check cancel_pressed is to do * so at completion of each row of output. */ -volatile bool cancel_pressed = false; +volatile sig_atomic_t cancel_pressed = false; static bool always_ignore_sigpipe = false; |
