Skip to content

fix: deflake //rs/state_manager:state_manager_integration - #11436

Merged
basvandijk merged 1 commit into
masterfrom
ai/deflake-state_manager_integration-2026-09-03
Sep 3, 2026
Merged

basvandijk merged 1 commit into
masterfrom
ai/deflake-state_manager_integration-2026-09-03

Conversation

@basvandijk

Copy link
Copy Markdown
Collaborator

Root cause

Since async hashing (#8464, re-landed in #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. #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.

Since async hashing (#10071), `commit_and_certify` with `Metadata` scope
returns before the `HashThread` has pushed the new snapshot. Four tests
committed a loop ending in a `Metadata`-scope height, called
`flush_tip_channel()` (which drains the tip thread, not the hash thread)
and then asserted `list_state_heights(CERT_ANY)` on the full height list,
so the last height was missing whenever the hash thread was descheduled.

Switch the four loops to `commit_and_certify_sync` and drop the outdated
"wait for hashing" comments.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

Copilot AI 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.

🟢 Approval recommended

The targeted synchronization correctly addresses the documented race without changing production behavior.

Pull request overview

Deflakes state-manager integration tests by ensuring asynchronous hashing completes before assertions and pruning.

Changes:

  • Uses commit_and_certify_sync in four affected test loops.
  • Removes outdated hashing comments while retaining required tip-thread flushing.
File summaries
File Description
rs/state_manager/tests/state_manager.rs Synchronizes hash completion in pruning tests and cleans up stale comments.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@basvandijk
basvandijk marked this pull request as ready for review September 3, 2026 13:43
@basvandijk
basvandijk requested a review from a team as a code owner September 3, 2026 13:43
@zeropath-ai

zeropath-ai Bot commented Sep 3, 2026

Copy link
Copy Markdown

✅ No security or compliance issues detected. Reviewed everything up to e6f3649.

Security Overview
Detected Code Changes
Change Type Relevant files
Enhancement ► rs/state_manager/tests/state_manager.rs
    Replace commit_and_certify with commit_and_certify_sync in multiple tests
► rs/state_manager/tests/state_manager.rs
    Remove comments about waiting for hashing to complete in several places

@basvandijk
basvandijk enabled auto-merge September 3, 2026 13:49
@basvandijk
basvandijk added this pull request to the merge queue Sep 3, 2026
Merged via the queue into master with commit 564b6df Sep 3, 2026
48 checks passed
@basvandijk
basvandijk deleted the ai/deflake-state_manager_integration-2026-09-03 branch September 3, 2026 14:26
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.

3 participants