New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle Ctrl-C better #231
Comments
On KeyboardInterrupt, send a cancel to the server and keep waiting for the result of the cancel, then re-raise KeyboardInterrupt. Only fixed on sync connections. Left a failing test for async connections; the test fails with an output from the script such as: error ignored in rollback on <psycopg.AsyncConnection [ACTIVE] ...>: sending query failed: another command is already in progress Traceback (most recent call last): File "<string>", line 27, in <module> File "/usr/lib/python3.8/asyncio/runners.py", line 44, in run return loop.run_until_complete(main) File "/usr/lib/python3.8/asyncio/base_events.py", line 603, in run_until_complete self.run_forever() File "/usr/lib/python3.8/asyncio/base_events.py", line 570, in run_forever self._run_once() File "/usr/lib/python3.8/asyncio/base_events.py", line 1823, in _run_once event_list = self._selector.select(timeout) File "/usr/lib/python3.8/selectors.py", line 468, in select fd_event_list = self._selector.poll(timeout, max_ev) KeyboardInterrupt And the except branch in AsyncConnection.wait() is not reached. See #231
On KeyboardInterrupt, send a cancel to the server and keep waiting for the result of the cancel, which is expected to raise a QueryCanceled, then re-raise KeyboardInterrupt. Before this, the connection was left in ACTIVE state, so it couldn't be rolled back. Only fixed on sync connections. Left a failing test for async connections; the test fails with an output from the script such as: error ignored in rollback on <psycopg.AsyncConnection [ACTIVE] ...>: sending query failed: another command is already in progress Traceback (most recent call last): File "<string>", line 27, in <module> File "/usr/lib/python3.8/asyncio/runners.py", line 44, in run return loop.run_until_complete(main) File "/usr/lib/python3.8/asyncio/base_events.py", line 603, in run_until_complete self.run_forever() File "/usr/lib/python3.8/asyncio/base_events.py", line 570, in run_forever self._run_once() File "/usr/lib/python3.8/asyncio/base_events.py", line 1823, in _run_once event_list = self._selector.select(timeout) File "/usr/lib/python3.8/selectors.py", line 468, in select fd_event_list = self._selector.poll(timeout, max_ev) KeyboardInterrupt And the except branch in `AsyncConnection.wait()` is not reached. See #231
Fixed for sync connections, but the same fix doesn't work for async ones. It seems that the Ctrl-C is trapped by
|
Which Python version are you using for development? I'm asking because of this bug which was fixed in Python 3.8: https://bugs.python.org/issue23057 |
I am personally developing on Python 3.8 on Linux, and the CI runs the tests on Python 3.6 to 3.10 on Linux, macOS, Windows. At the moment, this MR has problem:
The BPO issue you link seems about signals+windows but also signals+linux don't seem to obey as I hope yet |
On KeyboardInterrupt, send a cancel to the server and keep waiting for the result of the cancel, which is expected to raise a QueryCanceled, then re-raise KeyboardInterrupt. Before this, the connection was left in ACTIVE state, so it couldn't be rolled back. Only fixed on sync connections. Left a failing test for async connections; the test fails with an output from the script such as: error ignored in rollback on <psycopg.AsyncConnection [ACTIVE] ...>: sending query failed: another command is already in progress Traceback (most recent call last): File "<string>", line 27, in <module> File "/usr/lib/python3.8/asyncio/runners.py", line 44, in run return loop.run_until_complete(main) File "/usr/lib/python3.8/asyncio/base_events.py", line 603, in run_until_complete self.run_forever() File "/usr/lib/python3.8/asyncio/base_events.py", line 570, in run_forever self._run_once() File "/usr/lib/python3.8/asyncio/base_events.py", line 1823, in _run_once event_list = self._selector.select(timeout) File "/usr/lib/python3.8/selectors.py", line 468, in select fd_event_list = self._selector.poll(timeout, max_ev) KeyboardInterrupt And the except branch in `AsyncConnection.wait()` is not reached. See #231
On KeyboardInterrupt, send a cancel to the server and keep waiting for the result of the cancel, which is expected to raise a QueryCanceled, then re-raise KeyboardInterrupt. Before this, the connection was left in ACTIVE state, so it couldn't be rolled back. Only fixed on sync connections. Left a failing test for async connections; the test fails with an output from the script such as: error ignored in rollback on <psycopg.AsyncConnection [ACTIVE] ...>: sending query failed: another command is already in progress Traceback (most recent call last): File "<string>", line 27, in <module> File "/usr/lib/python3.8/asyncio/runners.py", line 44, in run return loop.run_until_complete(main) File "/usr/lib/python3.8/asyncio/base_events.py", line 603, in run_until_complete self.run_forever() File "/usr/lib/python3.8/asyncio/base_events.py", line 570, in run_forever self._run_once() File "/usr/lib/python3.8/asyncio/base_events.py", line 1823, in _run_once event_list = self._selector.select(timeout) File "/usr/lib/python3.8/selectors.py", line 468, in select fd_event_list = self._selector.poll(timeout, max_ev) KeyboardInterrupt And the except branch in `AsyncConnection.wait()` is not reached. See #231
On KeyboardInterrupt, send a cancel to the server and keep waiting for the result of the cancel, which is expected to raise a QueryCanceled, then re-raise KeyboardInterrupt. Before this, the connection was left in ACTIVE state, so it couldn't be rolled back. Only fixed on sync connections. Left a failing test for async connections; the test fails with an output from the script such as: error ignored in rollback on <psycopg.AsyncConnection [ACTIVE] ...>: sending query failed: another command is already in progress Traceback (most recent call last): File "<string>", line 27, in <module> File "/usr/lib/python3.8/asyncio/runners.py", line 44, in run return loop.run_until_complete(main) File "/usr/lib/python3.8/asyncio/base_events.py", line 603, in run_until_complete self.run_forever() File "/usr/lib/python3.8/asyncio/base_events.py", line 570, in run_forever self._run_once() File "/usr/lib/python3.8/asyncio/base_events.py", line 1823, in _run_once event_list = self._selector.select(timeout) File "/usr/lib/python3.8/selectors.py", line 468, in select fd_event_list = self._selector.poll(timeout, max_ev) KeyboardInterrupt And the except branch in `AsyncConnection.wait()` is not reached. See #231
Released in Psycopg 3.0.10. Currently untested on Windows (the test should be fixed) and not working in async mode, which I don't know if it can be solved. |
|
@dlax Later on I discovered |
Ah, thanks for the heads up; looks good then! |
Currently, if Ctrl-C is pressed during a long query, the connection is left in a broken state.
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.
The text was updated successfully, but these errors were encountered: