Repository navigation
Description
Summary
disttae can admit a stale immutable PartitionState after the logtail consumer has applied a table update and cleared that table's pending marker. The race was observed as a flaky mo_ctl mergeobjects failure in run 29838470048 / job 88667877201: the merge saw fewer than two objects and returned internal error: no matching objects, while metadata_scan one second later saw both unmerged objects.
The failure happened on PR #25970, but that PR only changes pkg/backup/file_index.go and its unit test. It does not touch the disttae, logtail, merge, or BVT paths involved here.
Root cause
txnTable.getPartitionState gets an immutable table snapshot through PushClient.toSubscribeTable, then calls waitCanServeTableSnapshot. The latter checks the already-captured snapshot first and independently reads subEntry.pendingTo through canServeTableSnapshotNoWait.
This leaves the following reachable TOCTOU window:
- A reader captures old partition snapshot
P0. - The logtail consumer applies the table update and publishes
P1. - The consumer clears
pendingTobecauseP1contains the update. - The reader checks
pendingTo, seesnil, and admitsP0through the no-pending fast path even thoughP0.appliedTo < snapshot.Prev().
The ordering contract needs to be the reverse: capture the pending marker before the immutable partition snapshot while holding the same subscription generation. Then either the marker is present and an old state waits, or the marker is absent and the later snapshot necessarily includes every update already cleared from pending.
PR #25515 added the pending-table fence but did not make these two reads atomic in the required order. PR #25888 already uses the correct pending-before-snapshot ordering for the separate PK-check path; ordinary getPartitionState reads still use the racy sequence.
Impact
Confirmed impact:
- flaky
mo_ctl('cn', 'mergeobjects', ...)failures after synchronous TN flushes; - a latest table read can transiently observe an older object set than the statement snapshot should admit.
The same mechanism is reachable by ordinary latest-state reads through txnTable.getPartitionState; the exact user-visible outcome depends on the query and on hitting the narrow apply/clear window.
Reproduction / evidence
The failed BVT has the exact signature of this interleaving:
mo_ctl_merge.testline 73 failed withinternal error: no matching objects;- line 75, after
sleep(1), returned two metadata rows withrows_cnt = 1instead of the single merged object withrows_cnt = 2; - the adjacent TKE BVT run 29838560063 passed the same suite;
- repeating the SQL sequentially on a local two-CN cluster 100 times passed, consistent with a narrow concurrency window rather than deterministic data or varchar-zone-map handling.
A deterministic unit regression can reproduce the state transition without timing:
- capture stale
PartitionStateP0; - set the table pending marker;
- publish a newer partition state
P1whoseappliedTocovers the read snapshot; - clear the pending marker;
- call the latest-state wait/admission path with
P0; - assert that it recaptures and returns
P1, notP0.
Current main returns P0 at step 6.
Proposed fix
Add a general latest-read snapshot capture that, under subscribedTable.rw.RLock, validates the table's database/subscription generation, reads pendingTo, and only then captures the immutable partition snapshot. Make waitCanServeTableSnapshot use that coupled result on every admission attempt. Preserve the current no-pending fast path, historical-checkpoint fallback, reconnect behavior, and context cancellation.
Do not paper over the symptom by retrying mergeobjects or adding a sleep to the BVT; both would leave ordinary latest reads exposed to the stale snapshot.
Acceptance criteria
- A pending marker is captured before its corresponding immutable partition snapshot under one subscription generation.
- A stale snapshot captured before apply cannot be admitted merely because the consumer has since cleared
pendingTo. - The no-pending fast path remains available for current snapshots.
- Unsubscribe/reconnect, historical snapshot fallback, and context cancellation retain their existing behavior.
- A deterministic regression test fails on current main and passes with the fix.
- Targeted and package-level
pkg/vm/engine/disttaetests pass, including race-enabled targeted coverage.
Activity
fix(disttae): recapture table state after pending logtail apply (#26009)
fix(disttae): recapture table state after pending logtail apply (matr…
fix(disttae): recapture table state after pending logtail apply (matr…
fix(disttae): recapture table state after pending logtail apply (matr…