Skip to content

Commit e589c48

Browse files
committed
Fix race condition in remove_temp_files_after_crash TAP test
The TAP test was written so that it was not waiting for the correct SQL command, but for output from the preceding one. This resulted in race conditions, allowing the commands to run in a different order, not block as expected and so on. This fixes it by inverting the order of commands where possible, so that observing the output guarantees the data was inserted properly, and waiting for a lock to appear in pg_locks. Discussion: https://postgr.es/m/CAH503wDKdYzyq7U-QJqGn%3DGm6XmoK%2B6_6xTJ-Yn5WSvoHLY1Ww%40mail.gmail.com
1 parent 27ab198 commit e589c48

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

src/test/recovery/t/022_crash_temp_files.pl

+42-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@
7979
# Insert one tuple and leave the transaction open
8080
$killme_stdin2 .= q[
8181
BEGIN;
82-
SELECT $$insert-tuple-to-lock-next-insert$$;
8382
INSERT INTO tab_crash (a) VALUES(1);
83+
SELECT $$insert-tuple-to-lock-next-insert$$;
8484
];
8585
pump_until($killme2, \$killme_stdout2, qr/insert-tuple-to-lock-next-insert/m);
8686
$killme_stdout2 = '';
@@ -100,6 +100,26 @@
100100
$killme_stdout = '';
101101
$killme_stderr = '';
102102

103+
# Wait until the batch insert gets stuck on the lock.
104+
$killme_stdin2 .= q[
105+
DO $c$
106+
DECLARE
107+
c INT;
108+
BEGIN
109+
LOOP
110+
SELECT COUNT(*) INTO c FROM pg_locks WHERE pid = ] . $pid . q[ AND NOT granted;
111+
IF c > 0 THEN
112+
EXIT;
113+
END IF;
114+
END LOOP;
115+
END; $c$;
116+
SELECT $$insert-tuple-lock-waiting$$;
117+
];
118+
119+
pump_until($killme2, \$killme_stdout2, qr/insert-tuple-lock-waiting/m);
120+
$killme_stdout2 = '';
121+
$killme_stderr2 = '';
122+
103123
# Kill with SIGKILL
104124
my $ret = TestLib::system_log('pg_ctl', 'kill', 'KILL', $pid);
105125
is($ret, 0, 'killed process with KILL');
@@ -147,8 +167,8 @@
147167
# Insert one tuple and leave the transaction open
148168
$killme_stdin2 .= q[
149169
BEGIN;
150-
SELECT $$insert-tuple-to-lock-next-insert$$;
151170
INSERT INTO tab_crash (a) VALUES(1);
171+
SELECT $$insert-tuple-to-lock-next-insert$$;
152172
];
153173
pump_until($killme2, \$killme_stdout2, qr/insert-tuple-to-lock-next-insert/m);
154174
$killme_stdout2 = '';
@@ -168,6 +188,26 @@
168188
$killme_stdout = '';
169189
$killme_stderr = '';
170190

191+
# Wait until the batch insert gets stuck on the lock.
192+
$killme_stdin2 .= q[
193+
DO $c$
194+
DECLARE
195+
c INT;
196+
BEGIN
197+
LOOP
198+
SELECT COUNT(*) INTO c FROM pg_locks WHERE pid = ] . $pid . q[ AND NOT granted;
199+
IF c > 0 THEN
200+
EXIT;
201+
END IF;
202+
END LOOP;
203+
END; $c$;
204+
SELECT $$insert-tuple-lock-waiting$$;
205+
];
206+
207+
pump_until($killme2, \$killme_stdout2, qr/insert-tuple-lock-waiting/m);
208+
$killme_stdout2 = '';
209+
$killme_stderr2 = '';
210+
171211
# Kill with SIGKILL
172212
$ret = TestLib::system_log('pg_ctl', 'kill', 'KILL', $pid);
173213
is($ret, 0, 'killed process with KILL');

0 commit comments

Comments
 (0)