*
* 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
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)
{
* 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;
{
const char *query = PQgetvalue(result, r, c);
- /* Abandon execution if CancelRequested */
- if (CancelRequested)
+ /* Abandon execution if cancel_pressed */
+ if (cancel_pressed)
goto loop_exit;
/*
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)
* 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;
}
#include "postgres_fe.h"
-#include <signal.h>
#include <unistd.h>
#include "fe_utils/cancel.h"
(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;