Skip to content

Commit ae542ff

Browse files
committed
Wait for the query to run before the cancel tests cancel it
A cancel that reaches the backend while it is still reading the query is dropped, and the query then runs to the end. The tests sent the query and cancelled within microseconds, and on a slow CI runner the cancel won: test_query_is_cancelled_when_psycopg_closed_the_connection failed once on Python 3.10. A user's Ctrl-C comes long after the query started, so only the tests change: they now wait until pg_stat_activity shows it active. Reproduced with a proxy that delays the query but not cancel requests: the old test fails there, the new one passes.
1 parent 93f26c3 commit ae542ff

1 file changed

Lines changed: 32 additions & 11 deletions

File tree

‎tests/test_cancel_recovery.py‎

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,45 @@
77
from utils import dbtest
88

99

10-
def _leave_query_unread(executor):
10+
def _start_sleep(executor, connection):
11+
"""Send pg_sleep(30) on the executor's connection, and return once it runs.
12+
13+
A cancel that reaches the backend before it starts the query, while it is
14+
still reading it, is dropped (ProcessInterrupts in postgres.c), and the
15+
query then runs to the end. A user's Ctrl-C comes long after, but a test
16+
cancelling right away could lose that race on a slow CI runner.
17+
"""
18+
pid = executor.conn.info.backend_pid
19+
executor.conn.pgconn.send_query(b"select pg_sleep(30)")
20+
deadline = time.monotonic() + 10
21+
while time.monotonic() < deadline:
22+
with connection.cursor() as cur:
23+
cur.execute("select state, query from pg_stat_activity where pid = %s", (pid,))
24+
row = cur.fetchone()
25+
if row and row[0] == "active" and "pg_sleep" in row[1]:
26+
return
27+
time.sleep(0.01)
28+
raise AssertionError("pg_sleep never started on the server")
29+
30+
31+
def _leave_query_unread(executor, connection):
1132
"""Put the connection where a second Ctrl-C leaves it.
1233
1334
psycopg handles the first Ctrl-C by sending a cancel and then reading the
1435
server's answer; a second Ctrl-C during that cleanup (easy over an SSH
1536
tunnel, where it takes a round trip or two) interrupts it, so the query is
1637
still running on the server and its answer is never read.
1738
"""
18-
executor.conn.pgconn.send_query(b"select pg_sleep(30)")
39+
_start_sleep(executor, connection)
1940
raise KeyboardInterrupt
2041

2142

2243
@dbtest
23-
def test_double_ctrl_c_does_not_leave_the_connection_busy(executor):
44+
def test_double_ctrl_c_does_not_leave_the_connection_busy(executor, connection):
2445
cli = PGCli(pgexecute=executor)
2546
start = time.monotonic()
2647

27-
with patch.object(cli, "_evaluate_command", side_effect=lambda text: _leave_query_unread(executor)):
48+
with patch.object(cli, "_evaluate_command", side_effect=lambda text: _leave_query_unread(executor, connection)):
2849
cli.execute_command("select pg_sleep(30)")
2950
output, query = cli._evaluate_command("select 40 + 2 as answer")
3051

@@ -37,13 +58,13 @@ def test_double_ctrl_c_does_not_leave_the_connection_busy(executor):
3758

3859

3960
@dbtest
40-
def test_connection_still_busy_after_the_cancel_is_closed_then_reopened(executor):
61+
def test_connection_still_busy_after_the_cancel_is_closed_then_reopened(executor, connection):
4162
cli = PGCli(pgexecute=executor)
4263
cli.auto_retry_closed_connection = True
4364

4465
with (
4566
patch.object(executor, "_drain_results", return_value=False),
46-
patch.object(cli, "_evaluate_command", side_effect=lambda text: _leave_query_unread(executor)),
67+
patch.object(cli, "_evaluate_command", side_effect=lambda text: _leave_query_unread(executor, connection)),
4768
):
4869
cli.execute_command("select pg_sleep(30)")
4970
assert executor.conn.closed
@@ -67,14 +88,14 @@ def _still_running(connection, pid):
6788
return True
6889

6990

70-
def _leave_query_running_and_closed(executor):
91+
def _leave_query_running_and_closed(executor, connection):
7192
"""Put the connection where psycopg >= 3.3.9 leaves it after a second Ctrl-C.
7293
7394
psycopg now closes the connection when its cancel is interrupted (psycopg
7495
#1435), so it is no longer busy but cannot cancel anything either, and the
7596
server keeps running the query until it next sends the client data.
7697
"""
77-
executor.conn.pgconn.send_query(b"select pg_sleep(30)")
98+
_start_sleep(executor, connection)
7899
executor.conn.pgconn.finish()
79100
raise KeyboardInterrupt
80101

@@ -85,7 +106,7 @@ def test_query_is_cancelled_when_psycopg_closed_the_connection(executor, connect
85106
cli.auto_retry_closed_connection = True
86107
pid = executor.conn.info.backend_pid
87108

88-
with patch.object(cli, "_evaluate_command", side_effect=lambda text: _leave_query_running_and_closed(executor)):
109+
with patch.object(cli, "_evaluate_command", side_effect=lambda text: _leave_query_running_and_closed(executor, connection)):
89110
cli.execute_command("select pg_sleep(30)")
90111

91112
assert not _still_running(connection, pid)
@@ -108,7 +129,7 @@ def test_another_ctrl_c_during_the_recovery_closes_the_connection(executor, conn
108129

109130
with (
110131
patch.object(executor, "recover_from_interrupt", side_effect=KeyboardInterrupt),
111-
patch.object(cli, "_evaluate_command", side_effect=lambda text: _leave_query_unread(executor)),
132+
patch.object(cli, "_evaluate_command", side_effect=lambda text: _leave_query_unread(executor, connection)),
112133
):
113134
cli.execute_command("select pg_sleep(30)")
114135

@@ -122,7 +143,7 @@ def test_ctrl_c_during_the_last_resort_cancel_is_ignored(executor, connection):
122143
# Someone hammering Ctrl-C must not cost the cancel: closing alone would
123144
# leave the query running on the server.
124145
pid = executor.conn.info.backend_pid
125-
executor.conn.pgconn.send_query(b"select pg_sleep(30)")
146+
_start_sleep(executor, connection)
126147
name = "cancel_safe" if hasattr(executor.conn, "cancel_safe") else "cancel"
127148
real_cancel = getattr(executor.conn, name)
128149

0 commit comments

Comments
 (0)