Skip to content

Handle Ctrl-C better #231

@dvarrazzo

Description

@dvarrazzo

Currently, if Ctrl-C is pressed during a long query, the connection is left in a broken state.

>>> import psycopg
>>> cnn = psycopg.connect()

>>> cnn.execute("select pg_sleep(5)")
^CTraceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/piro/dev/psycopg3/psycopg/psycopg/connection.py", line 808, in execute
    return cur.execute(query, params, prepare=prepare)
  File "/home/piro/dev/psycopg3/psycopg/psycopg/cursor.py", line 557, in execute
    self._conn.wait(
  File "/home/piro/dev/psycopg3/psycopg/psycopg/connection.py", line 860, in wait
    return waiting.wait(gen, self.pgconn.socket, timeout=timeout)
  File "/home/piro/dev/psycopg3/psycopg/psycopg/waiting.py", line 229, in wait_epoll
    fileevs = epoll.poll(timeout)
KeyboardInterrupt

>>> cnn.execute("select 1")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/piro/dev/psycopg3/psycopg/psycopg/connection.py", line 811, in execute
    raise ex.with_traceback(None)
psycopg.OperationalError: sending query failed: another command is already in progress

What pgcli does with psycopg2 makes sense: send a cancel and wait for the response. We should probably do the same by default in our wait functions, or maybe just out of them, because inside them we don't really have access to the PGconn structure.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions