Skip to content

Commit e61054a

Browse files
committed
Synchronise 039 on sync_event instead of fixed sleeps
Two sleep(2) calls stood in for synchronisation. The first waited for the apply worker to pick up a reloaded spock.exception_behaviour, the second for it to go idle before the cancel. Neither was guaranteed: the row arriving proves the commit is visible, but handle_commit() then calls process_syncing_tables(), which opens a transaction of its own, so the cancel could land outside the window the test is about and the test would pass without exercising the leak. Use spock.sync_event() and wait_for_sync_event() instead. A non-transactional sync event is decoded outside any remote transaction and ordered after the preceding commit, so the origin reaching its LSN means handle_commit() ran to the end -- replay queue reset, use_try_block cleared. It also orders the reload, since the worker tests ConfigReloadPending before reading from the stream. The same call replaces wait_for_row(), a 90s polling loop that burned its full timeout on failure; a plain SELECT after the sync does the job. Renumbered from 037, which PR #545 also claims, and added to tests/tap/schedule -- CI runs run_tests.sh, which is schedule-driven, so the test would never have been executed. 29 subtests to 33, passing, ~26s.
1 parent 2887651 commit e61054a

2 files changed

Lines changed: 24 additions & 15 deletions

File tree

‎tests/tap/schedule‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ test: 019_stale_fd_epoll_after_conn_death
4040
test: 021_uc_null_key_skip
4141
test: 022_apply_mem_context
4242
test: 025_tiebreaker_equal_warning
43+
test: 039_no_replay_mode_leak
4344
test: 045_lsn_from_commit_ts
4445

4546
# Regression tests
Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,16 @@ sub apply_worker_pid {
9797
return '';
9898
}
9999

100-
sub wait_for_row {
101-
my ($table, $id, $timeout) = @_;
102-
$timeout //= 90;
103-
for (1 .. $timeout) {
104-
my $c = scalar_query(2, "SELECT count(*) FROM $table WHERE id = $id");
105-
return $c if defined $c && $c eq '1';
106-
sleep(1);
107-
}
108-
return scalar_query(2, "SELECT count(*) FROM $table WHERE id = $id");
100+
# A non-transactional sync_event is decoded outside any remote transaction and
101+
# ordered after the preceding commit, so the origin reaching its LSN means the
102+
# worker finished handle_commit() -- replay queue reset, use_try_block cleared
103+
# -- and is back in the wait loop. A plain SELECT then sees what node 1 sent.
104+
sub sync_subscriber {
105+
my ($what) = @_;
106+
my $lsn = scalar_query(1, "SELECT spock.sync_event()");
107+
is(scalar_query(2,
108+
"CALL spock.wait_for_sync_event(NULL, 'n1', '$lsn'::pg_lsn, 30)"),
109+
't', $what);
109110
}
110111

111112
for my $phase (@modes) {
@@ -114,20 +115,24 @@ sub wait_for_row {
114115

115116
psql_or_bail(2, "ALTER SYSTEM SET spock.exception_behaviour = $mode");
116117
psql_or_bail(2, "SELECT pg_reload_conf()");
117-
sleep(2);
118118

119119
# A clean transaction first: it is what leaves a commit_lsn behind in the
120120
# worker's exception log slot, which is what the incoming transaction is
121121
# compared against after the cancel.
122122
psql_or_bail(1, "INSERT INTO $t VALUES (1, 'before cancel')");
123-
is(wait_for_row($t, 1), '1', "$mode: baseline transaction replicates");
123+
124+
# Leaves the worker between transactions with nothing queued, the state
125+
# the cancel needs. Also orders the reload: the worker tests
126+
# ConfigReloadPending before reading the stream, so processing this
127+
# message means it runs under this phase's spock.exception_behaviour.
128+
sync_subscriber("$mode: apply worker drained and idle before the cancel");
129+
130+
is(scalar_query(2, "SELECT count(*) FROM $t WHERE id = 1"), '1',
131+
"$mode: baseline transaction replicates");
124132

125133
my $pid_before = apply_worker_pid();
126134
like($pid_before, qr/^\d+$/, "$mode: found the apply worker ($pid_before)");
127135

128-
# The worker is idle here, so the cancel lands between transactions with
129-
# nothing queued -- the state the fix is about.
130-
sleep(2);
131136
my $log_offset = -s $subscriber_log // 0;
132137
is(scalar_query(2, "SELECT pg_cancel_backend($pid_before)"), 't',
133138
"$mode: cancel signal sent to the apply worker");
@@ -144,7 +149,10 @@ sub wait_for_row {
144149

145150
# The transaction that must not be treated as the recorded failure.
146151
psql_or_bail(1, "INSERT INTO $t VALUES (2, 'after cancel')");
147-
is(wait_for_row($t, 2), '1', "$mode: the next transaction applies");
152+
sync_subscriber("$mode: worker consumes the stream past the next transaction");
153+
154+
is(scalar_query(2, "SELECT count(*) FROM $t WHERE id = 2"), '1',
155+
"$mode: the next transaction applies");
148156

149157
my $log = read_log_from($log_offset);
150158
unlike($log, qr/applied nothing, retrying without exception handling/,

0 commit comments

Comments
 (0)