Skip to content

fix: Revert async hashing - #10001

Merged
basvandijk merged 2 commits into
masterfrom
mwe/revert
Apr 23, 2026
Merged

basvandijk merged 2 commits into
masterfrom
mwe/revert

Conversation

@michael-weigelt

Copy link
Copy Markdown
Contributor

This PR reverts #9850 and #8464 because the latter may have introduced a bug. The former depends on the latter, so reverting them together leads to the smallest conflict.

@michael-weigelt
michael-weigelt requested review from a team as code owners April 23, 2026 14:30
@github-actions github-actions Bot added the fix label Apr 23, 2026

@alin-at-dfinity alin-at-dfinity left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

☹️

@basvandijk
basvandijk enabled auto-merge April 23, 2026 15:32
@basvandijk
basvandijk added this pull request to the merge queue Apr 23, 2026
Merged via the queue into master with commit ebfe8f0 Apr 23, 2026
64 of 69 checks passed
@basvandijk
basvandijk deleted the mwe/revert branch April 23, 2026 15:51
AleDema pushed a commit to AleDema/ic that referenced this pull request Oct 18, 2026
…nable_chain_keys_test_farm_colocate (dfinity#11818)

## Problem


`//rs/tests/consensus/subnet_recovery:sr_app_no_upgrade_enable_chain_keys_test_farm_colocate`
flaked once in the week up to 2026-10-11 ([2026-10-05 16:19
UTC](https://dash.dm1-idx1.dfinity.network/invocation/b6dca5c2-ade4-4df4-94c4-d5d47bda5ff1?target=//rs/tests/consensus/subnet_recovery:sr_app_no_upgrade_enable_chain_keys_test_farm_colocate),
on PR dfinity#11744's branch). The recovery tool failed in
`ValidateReplayOutput`:

```
Execution of step ValidateReplayOutput failed: Output error: Invalid output: Replay height smaller than certification height.
```

`ic-replay` had replayed 45 batches up to height 245 (the certification
height), delivered the extra checkpointing batch 246 and logged `Created
unverified checkpoint @246`, `Computed root hash ... of state @246` and
`Validated checkpoint @246`. Yet its `ReplayOutput` reported the latest
CUP's state, `StateParams { height: 200, hash: f3f4ff1b... }`, so the
validation compared 200 - 1 against 245. In the passing attempts `Height
after replay` is the certification height + 1, as intended since dfinity#11203.

## Root cause

`Player::get_latest_state_params` read the hash of the latest state once
and treated *any* `Err` from `get_state_hash_at` as "the latest state
exists in memory only", falling back to the latest CUP. That read can
fail transiently for a checkpoint that already exists on disk:

- For a checkpoint round, `commit_and_certify` enqueues
`ComputeManifest` and `ValidateReplicatedStateAndFinalize` on the tip
channel, then has the hash thread insert `states_metadata[height]`
(without a manifest yet) and publish the new `latest_state_height`,
waits for it, and only then enqueues the follow-up `WaitForManifest`
request that publishes the manifest (and `ResetTipAndMerge`).
- `ic-replay`'s `wait_for_state` polls `latest_state_height` every 500
ms and then calls `flush_tip_channel`. If it observes the new height
before `WaitForManifest` is enqueued, its `Wait` request lands between
`ValidateReplicatedStateAndFinalize` and `WaitForManifest`: the flush
returns as soon as the checkpoint is validated, while the root hash is
still unpublished, and `get_state_hash_at` returns
`Transient(HashNotComputedYet)`.

The failing log shows this interleaving. Checkpoint 246 was created and
its certification hash computed at 16:19:36.635; `ic-replay` saw height
246 at .664 (one poll later) and its flush returned at .797, right after
`Validated checkpoint @246`; two further `wait_for_state` calls at .797
and .798 returned immediately, so `WaitForManifest` was still not
enqueued; `ReplayOutput { height: 200 }` was printed at .975, and
`ResetTipAndMerge` (enqueued together with `WaitForManifest`) ran at
.969, during the state manager's drop. Why `commit_and_certify` needed
more than 160 ms between publishing the height and enqueueing the
follow-ups is inferred, not shown: the hash thread persists the states
metadata with an fsync before it answers, and this run's checkpoint
creation took 470 ms against 10 ms in the passing attempt.

The window exists since async hashing moved the height publication onto
the hash thread (dfinity#8464, reverted in dfinity#10001, re-landed as dfinity#10071 on
2026-05-08); the single read in `ic-replay` dates from 2022.

This is not only a flake: the recovery tool went on to delete every
checkpoint but the highest (246) while the state params it would put
into the recovery CUP were those of height 200. `ValidateReplayOutput`
caught it here, but the corrupted-CUP recoveries skip that step.

## Fix

Read the hash with the existing `get_state_hash` helper, which retries
transient errors every second and returns `None` only for
`StateNotFullyCertified`, i.e. a state that really exists in memory
only; the fallback to the CUP (or, without a consensus pool, to the
highest checkpoint, now read the same way) is unchanged for that case.
The helper answers height 0, which is never a checkpoint, with `None`
without polling (an empty state directory would otherwise make the state
manager report it as not committed yet forever), so
`update-registry-local-store` on an empty state fails as before instead
of waiting. Unit tests pin both `None` cases down.

Behaviour changes to be aware of: a `StateRemoved` error for the latest
state, which cannot happen, now panics instead of silently reporting the
CUP; and like `verify_latest_cup`, which already used the helper, the
poll is unbounded, so a manifest that is never published would make
`ic-replay` log "Waiting for state hash" every second rather than report
a wrong height.

Follow-up for the state manager (not in this PR): `flush_tip_channel` is
documented to finish "all asynchronous checkpointing operations,
including ... manifest computation", but a caller on another thread can
flush before `commit_and_certify` has enqueued `WaitForManifest`.
`ic-replay` was the only such caller; the other callers flush from the
committing thread.

Not related to dfinity#11795: that change only touches the Local backend's
bridge, and this run was colocated on Farm.

## Testing

- `cargo check --all-targets --all-features -p ic-replay`, `cargo fmt`,
`./ci/scripts/rust-lint.sh`.
- `bazel build //rs/replay:replay`; `bazel test
//rs/recovery:recovery_test //rs/replay:player_integration_tests
//rs/replay:replay_doc_test //rs/replay:replay_test` (rerun after each
review follow-up).
- `bazel test --runs_per_test=3 --jobs=3
//rs/tests/consensus/subnet_recovery:sr_app_no_upgrade_enable_chain_keys_test_farm_colocate`:
3/3 passed on Farm (372 s, 390 s, 380 s), each with `Height after
replay` = certification height + 1. Passing runs cannot exercise the
window, which needs the checkpoint's I/O to be slow; the evidence for
the fix is the log analysis above and the unit tests.

This PR was created following the steps in
`.claude/skills/fix-flaky-tests/SKILL.md`.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Fable 5.1 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants