Skip to content

[Bug]: disttae can admit a stale PartitionState after pending logtail clears #26006

Closed

Description

@LeftHandCold

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:

  1. A reader captures old partition snapshot P0.
  2. The logtail consumer applies the table update and publishes P1.
  3. The consumer clears pendingTo because P1 contains the update.
  4. The reader checks pendingTo, sees nil, and admits P0 through the no-pending fast path even though P0.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.test line 73 failed with internal error: no matching objects;
  • line 75, after sleep(1), returned two metadata rows with rows_cnt = 1 instead of the single merged object with rows_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:

  1. capture stale PartitionState P0;
  2. set the table pending marker;
  3. publish a newer partition state P1 whose appliedTo covers the read snapshot;
  4. clear the pending marker;
  5. call the latest-state wait/admission path with P0;
  6. assert that it recaptures and returns P1, not P0.

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/disttae tests pass, including race-enabled targeted coverage.

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions