Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ Bug fixes:
created by ``uv venv``.
* Read ``alias_map_file`` as UTF-8 (with or without a BOM) instead of the
locale encoding, which made non-ASCII aliases fail on Windows.
* A second Ctrl-C while a query is being cancelled no longer leaves the
connection stuck. psycopg answers Ctrl-C by cancelling the query and then
reading the server's reply; another Ctrl-C during that cleanup, easy over an
SSH tunnel where it takes a round trip or two, cut it short, so the cancel
could be lost, the query kept running on the server and every later
statement failed with ``another command is already in progress``. After a
cancelled query pgcli now checks the connection: if it is still busy it
cancels again and reads the reply, and if the server does not answer within
5 seconds, or yet another Ctrl-C arrives, it cancels (ignoring Ctrl-C for
the at most 2 seconds that takes, so the query never outlives the session)
and closes the connection so the next statement reconnects. Since psycopg
3.3.9 the second Ctrl-C makes psycopg close the connection itself, which
left the query running on the server; pgcli then cancels it with the key
libpq gave when connecting, which still reaches the backend.

Internal:
---------
Expand Down
39 changes: 39 additions & 0 deletions pgcli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,7 @@ def execute_command(self, text, handle_closed_connection=True):
else:
logger.debug("cancelled query, sql: %r", text)
click.secho("cancelled query", err=True, fg="red")
self._recover_from_interrupt()
except NotImplementedError:
click.secho("Not Yet Implemented.", fg="yellow")
except OperationalError as e:
Expand Down Expand Up @@ -1450,6 +1451,44 @@ def _evaluate_command(self, text):

return output, meta_query

def _recover_from_interrupt(self):
"""Make sure Ctrl-C did not leave the connection busy.

See PGExecute.recover_from_interrupt(). If yet another Ctrl-C lands
while that runs, give the connection up rather than leave it stuck.
"""
if self.pgexecute is None:
return
try:
outcome = self.pgexecute.recover_from_interrupt()
except KeyboardInterrupt:
self.pgexecute.abandon_connection()
outcome = "closed"
if outcome == "recancelled":
self.logger.debug("psycopg closed the connection mid-cancel; cancel sent again")
click.secho(
"The connection was closed while the query was being cancelled; the cancel has been sent "
"again. The next statement reconnects.",
err=True,
fg="red",
)
elif outcome == "lost":
self.logger.debug("psycopg closed the connection mid-cancel; the cancel could not be sent")
click.secho(
"The connection was closed while the query was being cancelled, and the query may still be "
"running on the server. The next statement reconnects.",
err=True,
fg="red",
)
elif outcome == "closed":
self.logger.debug("Connection closed after an interrupted query")
click.secho(
"The connection was still busy after the cancel and has been closed. If the cancel did not "
"get through, the query may still be running on the server. The next statement reconnects.",
err=True,
fg="red",
)

def _handle_server_closed_connection(self, text):
"""Used during CLI execution."""
try:
Expand Down
155 changes: 155 additions & 0 deletions pgcli/pgexecute.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import contextlib
import ipaddress
import logging
import select
import signal
import threading
import time
import traceback
from collections import namedtuple
import re
Expand Down Expand Up @@ -56,6 +61,31 @@ def register_typecasters(connection):


# pg3: I don't know what is this
@contextlib.contextmanager
def _sigint_ignored():
"""Ignore Ctrl-C inside the block (main thread only, where signals live)."""
try:
previous = signal.signal(signal.SIGINT, signal.SIG_IGN)
except ValueError:
# Not the main thread: signal handlers cannot be changed here.
yield
return
try:
yield
finally:
# None means the old handler was not set from Python.
signal.signal(signal.SIGINT, previous if previous is not None else signal.default_int_handler)


def _cancel_handle_of(conn):
"""libpq's cancel key for ``conn``'s backend, or None if it has none."""
try:
return conn.pgconn.get_cancel()
except Exception as e:
_logger.debug("No cancel key for the connection: %r", e)
return None


class ProtocolSafeCursor(psycopg.Cursor):
"""This class wraps and suppresses Protocol Errors with pgbouncer database.
See https://github.com/dbcli/pgcli/pull/1097.
Expand Down Expand Up @@ -172,6 +202,8 @@ def __init__(
self._conn_params = {}
self._is_virtual_database = None
self.conn = None
# libpq's cancel key for self.conn's backend; see send_saved_cancel().
self._cancel_handle = None
self.dbname = None
self.user = None
self.password = None
Expand Down Expand Up @@ -238,6 +270,8 @@ def connect(
self.conn.close()
self.conn = conn
self.conn.autocommit = True
# Taken while the connection is open: psycopg may close it mid-cancel.
self._cancel_handle = _cancel_handle_of(conn)

if self.notify_callback is not None:
self.conn.add_notify_handler(self.notify_callback)
Expand Down Expand Up @@ -448,6 +482,127 @@ def _must_raise(self, e):
"""
return self.conn.closed != 0

def recover_from_interrupt(self, timeout=5.0):
"""Leave the connection usable after Ctrl-C interrupted a query.

psycopg answers the first Ctrl-C by cancelling the query and then
reading the server's reply. A second Ctrl-C during that cleanup, easy
over an SSH tunnel where it takes a round trip or two, interrupts it:
the cancel may never be sent and the reply is never read, so every
later statement fails with "another command is already in progress"
while the query keeps running on the server.

Returns "idle" when the connection was not busy, "drained" once the
query is cancelled and its reply read, or "closed" when the server did
not answer within ``timeout`` and the connection had to be given up,
so that the next statement reconnects. When psycopg already closed the
connection, returns "recancelled" once the cancel saved at connect
time went out, or "lost" if it could not be sent.
"""
conn = self.conn
if conn is None:
return "idle"
if conn.closed:
# psycopg >= 3.3.9 closes the connection itself when a second
# Ctrl-C interrupts its cancel (psycopg #1435). The connection can
# no longer cancel anything, and the server only notices the
# client is gone when it next sends data.
if self._cancel_handle is None:
return "idle"
return "recancelled" if self.send_saved_cancel() else "lost"
if conn.info.transaction_status != psycopg.pq.TransactionStatus.ACTIVE:
return "idle"
try:
# cancel_safe() is psycopg >= 3.2; cancel() blocks but works on 3.0.
if hasattr(conn, "cancel_safe"):
conn.cancel_safe(timeout=timeout)
else:
conn.cancel()
except Exception as e:
_logger.debug("Cancel after an interrupted query failed: %r", e)
if self._drain_results(timeout):
return "drained"
_logger.debug("Connection still busy %ss after the cancel, closing it", timeout)
conn.close()
return "closed"

def abandon_connection(self, timeout=2.0):
"""Close the connection, first cancelling whatever it still runs.

For when the user interrupts the recovery itself: closing alone would
leave the query running on the server, which only notices the client
is gone when it next sends it data. Ctrl-C is ignored for the at most
``timeout`` seconds the cancel takes, so it always goes out.
"""
conn = self.conn
if conn is None:
return
if conn.closed:
self.send_saved_cancel(timeout)
return
try:
if conn.info.transaction_status == psycopg.pq.TransactionStatus.ACTIVE:
with _sigint_ignored():
if hasattr(conn, "cancel_safe"):
conn.cancel_safe(timeout=timeout)
else:
conn.cancel()
except (Exception, KeyboardInterrupt) as e:
# Best effort: the connection is closed below either way.
_logger.debug("Cancel before closing failed: %r", e)
finally:
conn.close()

def send_saved_cancel(self, timeout=2.0):
"""Cancel the backend's query with the key saved at connect time.

It still works once the connection is closed. Ctrl-C is ignored while
it is sent, for at most ``timeout`` seconds; the key is used only once,
since the backend goes away with the closed connection. Returns True
if the cancel request went out.
"""
handle, self._cancel_handle = self._cancel_handle, None
if handle is None:
return False
sent = threading.Event()

def send():
try:
handle.cancel()
sent.set()
except Exception as e:
_logger.debug("Cancel with the saved key failed: %r", e)

# In a thread because libpq's cancel blocks with no timeout of its own.
thread = threading.Thread(target=send, name="pgcli-cancel", daemon=True)
with _sigint_ignored():
thread.start()
thread.join(timeout)
return sent.is_set()

def _drain_results(self, timeout):
"""Read and discard the reply of the query in flight.

Waits at most ``timeout`` seconds; returns True once the connection is
idle again.
"""
pgconn = self.conn.pgconn # type: ignore[union-attr]
deadline = time.monotonic() + timeout
try:
while True:
pgconn.consume_input()
if pgconn.is_busy():
remaining = deadline - time.monotonic()
if remaining <= 0:
return False
select.select([pgconn.socket], [], [], remaining)
continue
if pgconn.get_result() is None:
return True
except psycopg.Error as e:
_logger.debug("Draining an interrupted query failed: %r", e)
return False

def execute_normal_sql(self, split_sql):
"""Returns tuple (title, rows, headers, status)"""
_logger.debug("Regular sql statement. sql: %r", split_sql)
Expand Down
Loading
Loading