Skip to content

Fix Keeper crash on stale snapshot chunk request - #107141

Closed
groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:fix-keeper-snapshot-null-ctx-crash
Closed

groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:fix-keeper-snapshot-null-ctx-crash

Conversation

@groeneai

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix a Keeper server crash (Logical error: 'false') that could happen during snapshot transfer to a recovering follower while snapshots were rotating. A stale snapshot chunk request is now declined instead of aborting the server.

Description

IKeeperStateMachine::read_logical_snp_obj aborted with chassert(false) when NuRaft called it with obj_id > 0 and a null user_snp_ctx.

That state is reachable during snapshot rotation under write load. The obj_id == 0 request can return -1 without creating a transfer context (the requested snapshot was retired so its pin is missing, or the loader failed to init). NuRaft then resets the peer's snapshot_sync_ctx but keeps its offset > 0, so the next async_io_loop iteration asks for a later chunk while the fresh context has user_snp_ctx == nullptr, and the leader aborts.

This is a legitimate stale request, not an invariant violation, so the chunk is now declined the same way as the existing missing-pin deferral: free the context, count it as KeeperReadSnapshotDeferred, and return -1. NuRaft restarts the install from obj_id 0 against the latest snapshot. Marking it as a deferral rather than a failure also stops it from inflating KeeperReadSnapshotFailed.

Introduced by #104941. Surfaced by the test_keeper_snapshot_rotation_race integration test on master (Logical error: 'false' in read_logical_snp_obj, and KeeperReadSnapshotFailed delta = 1). Added a deterministic unit test (ReadSnapshotChunkWithoutContextDoesNotCrash) that drives the exact state: it aborts the server on the current code and passes with this fix.

read_logical_snp_obj aborted with a chassert(false) when called with
obj_id > 0 and a null user_snp_ctx. That state is reachable during
snapshot rotation: the obj_id == 0 request can return -1 without
creating a transfer context (the requested snapshot was retired so its
pin is missing, or the loader failed to init). NuRaft then resets the
peer's snapshot_sync_ctx but keeps its offset > 0, so the next
async_io_loop iteration asks for a later chunk while the fresh context
has user_snp_ctx == nullptr, and the leader aborts.

This is a legitimate stale request, not an invariant violation. Decline
it the same way as the missing-pin deferral (free the context, count it
as KeeperReadSnapshotDeferred, return -1); NuRaft restarts the install
from obj_id 0 against the latest snapshot. Marking it as a deferral
rather than a failure also stops it from inflating
KeeperReadSnapshotFailed.

Add a unit test that drives read_logical_snp_obj with obj_id > 0 and no
context and asserts it declines instead of crashing, then verifies a
normal transfer still succeeds.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate:

# Check Result
a Deterministic repro? Yes. New unit test CoordinationTest.ReadSnapshotChunkWithoutContextDoesNotCrash calls read_logical_snp_obj(s, user_snp_ctx=nullptr, obj_id=1, ...). On current code it aborts (SIGABRT, exit 134) via chassert(false) at KeeperStateMachine.cpp:1549.
b Root cause explained? Yes. During snapshot rotation the obj_id == 0 request returns -1 without creating a transfer context (retired snapshot -> missing pin, or loader init fails). NuRaft resets the peer's snapshot_sync_ctx but keeps offset > 0, so the next async_io_loop call asks for a later chunk with a fresh user_snp_ctx == nullptr, hitting the assertion.
c Fix matches root cause? Yes. The null-context chunk request is a legitimate stale request, so it is declined (free ctx, count as KeeperReadSnapshotDeferred, return -1) exactly like the existing missing-pin deferral. NuRaft restarts the install from obj_id 0. No assertion is widened or disabled elsewhere.
d Test intent preserved / new test added? New unit test added; no existing test weakened. The other chassert at L1572 (valid ctx, obj_id out of file range) is a different invariant and is left intact.
e Demonstrated both directions? Yes. Without fix: test aborts (exit 134). With fix: all 4 storage parametrizations PASS; full Coordination snapshot suite 85/85 PASS (incl. TestReadSnapshotParallelMultiChunk).
f Fix is general, not a narrow patch? read_logical_snp_obj is the only consumer of user_snp_ctx; the only sibling early-return paths already set success correctly and the missing-pin deferral already returns -1. Marking this path as a deferral also resolves the sibling symptom of KeeperReadSnapshotFailed being inflated (seen as KeeperReadSnapshotFailed delta = 1 on master in test_keeper_snapshot_rotation_race).

Surfaced by test_keeper_snapshot_rotation_race on master (Logical error: 'false' in read_logical_snp_obj; arm_binary master hits 2026-06-09). Introduced by #104941.

session: cron:clickhouse-worker-slot-24:20260611-015100

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @antonio2368 @Michicosun - could you review? This fixes a Keeper leader crash (chassert(false) in read_logical_snp_obj) when NuRaft requests a snapshot chunk with obj_id > 0 after the peer's sync context was reset during rotation. The stale chunk is now declined as a deferral instead of aborting. Introduced by #104941; surfaced by test_keeper_snapshot_rotation_race on master.

@antonio2368

Copy link
Copy Markdown
Member

@groeneai check if the last failures were actually fixed by #106842

@groeneai

Copy link
Copy Markdown
Collaborator Author

@antonio2368 Yes, the data confirms #106842 fixed the runtime trigger.

CIDB on test_keeper_snapshot_rotation_race, split at the #106842 merge (2026-06-10 13:36:59 UTC):

  • Before: 60 failures (5 master, 55 PR), last at 2026-06-10 03:37.
  • After: 0 failures, with 2324 OK runs (230 master, 2094 PR), so it is running and not skipped.

Broader crash signature (read_logical_snp_obj with null user_snp_ctx): 18 hits before, 1 after. That single "after" hit (PR #106821, test_keeper_snapshot_chunked_transfer, 14:06 UTC) ran on a build that predates the fix: its build commit 02d11843 was committed at 12:26 UTC, and the #106842 merge is 28 commits ahead of it (the branch had not picked up master yet). So every real failure ran on a build without #106842.

So disabling nuraft_use_bg_thread_for_snapshot_io by default removes the path that drove the stale chunk request, and the crash is gone in the default config.

On #107141 vs #106842: they address different layers. #106842 turns the trigger off in the default config. #107141 hardens read_logical_snp_obj itself so the leader declines the stale chunk instead of chassert(false). The unit test in the PR drives that state directly and still aborts on current master, so the abort is still reachable if nuraft_use_bg_thread_for_snapshot_io is re-enabled.

Your call as Keeper owner: keep #107141 as defense-in-depth (it only converts the abort into the existing deferral path), or close it if off-by-default is enough. Either works for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants