Skip to content

fix: Treat certifications like other in-memory states to keep around when pruning. Reverts the revert of async hashing. - #10071

Merged
michael-weigelt merged 18 commits into
masterfrom
mwe/async_hashing_2
May 8, 2026
Merged

michael-weigelt merged 18 commits into
masterfrom
mwe/async_hashing_2

Conversation

@michael-weigelt

@michael-weigelt michael-weigelt commented Apr 30, 2026 •

Copy link
Copy Markdown
Contributor

When pruning states below a certain height, we keep around some special heights anyway (such as the tip_height). This must be true for both certification_metadata and certifications.

This also reverts the revert of #8464

@michael-weigelt
michael-weigelt requested review from a team as code owners April 30, 2026 11:49
@github-actions github-actions Bot added the fix label Apr 30, 2026
Comment thread rs/state_manager/tests/state_manager.rs Resolved
Comment thread rs/state_manager/src/lib.rs Resolved
Comment thread rs/state_manager/src/lib.rs Resolved
Comment thread rs/state_manager/src/lib.rs Resolved
Comment thread rs/state_manager/src/lib.rs Resolved
Comment thread rs/state_manager/src/lib.rs Resolved
@schneiderstefan

Copy link
Copy Markdown
Contributor

Does this intentionally also revert the revert? Can't the fix go in independently?

Comment thread rs/state_manager/tests/state_manager.rs Resolved
Comment thread rs/state_manager/tests/state_manager.rs Resolved
@mraszyk

mraszyk commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

Does this intentionally also revert the revert? Can't the fix go in independently?

I think it's better to revert the revert so that we don't need to separate the changes...

@schneiderstefan

Copy link
Copy Markdown
Contributor

I think it's better to revert the revert so that we don't need to separate the changes...

That's ok, but then the title and description should reflect that too.

@michael-weigelt michael-weigelt changed the title fix: Treat certifications like other in-memory states to keep around when pruning. fix: Treat certifications like other in-memory states to keep around when pruning. Reverts the revert of async hashing. May 4, 2026
Comment thread rs/state_manager/tests/state_manager.rs Resolved
Comment thread rs/state_machine_tests/src/lib.rs Resolved
Comment thread rs/state_manager/src/lib.rs Resolved
Comment thread rs/state_manager/tests/state_manager.rs Resolved
Comment thread rs/state_manager/tests/state_manager.rs Resolved
Comment thread rs/state_manager/tests/state_manager.rs Resolved
Comment thread rs/state_manager/src/lib.rs Resolved
@michael-weigelt
michael-weigelt requested a review from a team as a code owner May 8, 2026 07:35
@michael-weigelt
michael-weigelt added this pull request to the merge queue May 8, 2026
Merged via the queue into master with commit ed8823d May 8, 2026
35 checks passed
@michael-weigelt
michael-weigelt deleted the mwe/async_hashing_2 branch May 8, 2026 08:38
pull Bot pushed a commit to bit-cook/ic that referenced this pull request Sep 3, 2026
)

## Root cause

Since async hashing (dfinity#8464, re-landed in dfinity#10071 on 2026-05-08),
`commit_and_certify` with `CertificationScope::Metadata` returns before
the `HashThread` has pushed the new state into `states.snapshots`,
inserted its `certifications_metadata` entry and bumped
`latest_state_height`. Only `Full`-scope commits await the hash thread.

Four tests commit a loop whose last height is `Metadata` scope, call
`flush_tip_channel()` and then assert `list_state_heights(CERT_ANY)`
equals every committed height:

- `can_purge_intermediate_snapshots` (failed 3x on CI in the last week)
- `can_keep_the_latest_snapshot_after_removal` (1x)
- `remove_inmemory_states_below_can_keep_extra_states` (1x)
- `can_remove_checkpoints_and_noncheckpoints_separately` (identical
shape, not yet observed)

`flush_tip_channel()` drains the tip thread (checkpointing, manifest,
validation), not the hash thread, so when the hash thread is descheduled
on a loaded CI runner the assertion sees the last height missing:

```
thread 'can_purge_intermediate_snapshots' panicked at rs/state_manager/tests/state_manager.rs:2347:9:
assertion `left == right` failed
  left: [0, 1, ..., 20, 21]
 right: [0, 1, ..., 20, 21, 22]
```

Only the last height can ever be missing, because each subsequent
`commit_and_certify` needs the previous state hash and blocks on a
`HashRequest::Wait` for it.

The comment above the `flush_tip_channel()` calls ("We need to wait for
hashing to complete...") dates from 2023, when the tip thread did the
manifest hashing and `commit_and_certify` inserted the snapshot
synchronously. dfinity#10131 introduced `commit_and_certify_sync` and converted
most tests, but left these four loops on the async variant.

Reproduced deterministically by inserting a 3 s sleep for
`Metadata`-scope requests at the top of the `HashThread`'s `HashState`
arm: all four tests fail with byte-identical output to CI; with a
hash-thread barrier they pass under the same delay.

## Fix

Switch the four loops to `commit_and_certify_sync`, which is
`commit_and_certify` followed by `flush_hash_channel()`. Remove the
misleading "wait for hashing" comments (also from
`cannot_remove_latest_height_or_checkpoint`, which already used
`_sync`). The `flush_tip_channel()` calls stay: the pruning assertions
that follow still depend on checkpointing having finished.

Not addressed here (separate root cause, 1 of the last 6 flaky runs):
the 19-test `Checkpoint @n didn't complete in 300s` failure mode, where
the filesystem-wide `syncfs` in `finalize_and_remove_unverified_marker`
blocks every checkpoint validation in the process while the CI runner's
disk drains unrelated dirty pages.

## Verification

```
bazel test --test_output=errors --runs_per_test=3 --jobs=3 //rs/state_manager:state_manager_integration
```

All 3 runs pass (172 passed each, 40 to 41 s). Also run: `cargo check
--all-targets --all-features -p ic-state-manager`, `cargo fmt`,
`./ci/scripts/rust-lint.sh` (clean), `bazel build` and `bazel test
--test_output=errors` on the directly affected targets.

With a temporary 3 s delay injected into the hash thread for
`Metadata`-scope requests, the four tests fail deterministically on
`master` and pass with this change.

---

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

Co-authored-by: Claude Fable 5.1 <[email protected]>
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants