Repository navigation
fix: deflake //rs/tests/consensus/subnet_recovery:sr_nns_failover_nodes_test_local - #12116
Conversation
…es_test_local The recovery tool restarts setup-permissions after replacing ic_state, but only ic-replica is stopped at that point. The crypto vault keeps running and can still be finishing a secret key store write for the stopped replica (sks_data.pb.tmp/.old). When setup-permissions walks /var/lib/ic/crypto during such a write, chown/chmod fail on the vanished files, the script aborts before it re-owns ic_state, and the replica crash-loops with PermissionDenied on ic_state/page_deltas. Keep the crypto vault stopped while setup-permissions runs, as at boot: stop ic-crypto-csp.socket and ic-crypto-csp.service before restarting setup-permissions, so that a connection can't socket-activate the vault, and start them again before ic-replica. This applies to UploadStateAndRestartStep and UploadCUPAndTarStep. Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues were identified for the recovery permission-ordering change.
No open issues 🎉
What changed in this PR
Fixes the permissions race behind the flaky NNS failover recovery test by stopping the crypto vault while ic-recovery restores permissions on uploaded state.
Changes:
- Stops the vault service and socket before
setup-permissions, then restarts both before the replica. - Applies this sequence to local and remote state uploads and
UploadCUPAndTarStep. - Documents the ordering requirement and updates the recovery step description.
| File | Description |
|---|---|
rs/recovery/src/steps.rs |
Stops vault activity during permission setup in both recovery command sequences. |
Review effort: Balanced
pierugo-dfinity
left a comment
There was a problem hiding this comment.
StopReplicaStep and ic_subnet_merging::steps::StopReplicaAtHaltingCheckpointStep also stop the replica, we should probably also stop crypto there.
Like Leo suggests, we could try to put those in a single place and reuse them where possible. We could also move the responsibility to systemd by changing the dependencies of those two units, but that has a bigger impact
CMD_STOP_REPLICA now stops ic-replica and then the crypto vault and its socket, and CMD_RESTART_REPLICA re-runs setup-permissions before starting the vault and ic-replica again. Both are module-level constants, and CMD_RESTART_REPLICA separates its commands with newlines, so UploadCUPAndTarStep interpolates both into its script instead of keeping its own copy. StopReplicaStep and the subnet merging tool's StopReplicaAtHaltingCheckpointStep use CMD_STOP_REPLICA as well, so every step that stops the replica leaves the node in the state CMD_RESTART_REPLICA expects. Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
🔵 Needs a closer look
Crypto-owner confirmation is needed that interrupting vault writes is crash-safe across supported GuestOS versions, since the forced-race test verifies service ordering rather than interrupted real key-store writes.
No open issues 🎉
Review effort: Balanced
…ault Explain why CMD_STOP_REPLICA stops the vault's socket and joins its two commands with &&, and mention the crypto vault in the remaining texts that describe steps which stop the replica. Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
🔵 Needs a closer look
Crypto-maintainer confirmation is needed for crash/restart safety across affected recovery and subnet-merging operations and deployed GuestOS versions, beyond the inspected key-store atomic-write path.
No open issues 🎉
Review effort: Balanced
ic-recoverynow stops the crypto vault (ic-crypto-csp.socketandic-crypto-csp.service) right afteric-replica. It starts the vault again only after it has re-runsetup-permissions, which hands the replacedic_statetoic-replica. So, as at boot, the vault is not running whilesetup-permissionsruns. The commands are two shared constants inic_recovery::steps:Every step that stops the replica uses
CMD_STOP_REPLICA:UploadStateAndRestartStep, in bothRemoteandLocalmode;UploadCUPAndTarStep;StopReplicaStep, the first step of both NNS recoveries;StopReplicaAtHaltingCheckpointStep.UploadStateAndRestartStepandUploadCUPAndTarStepthen restart the node withCMD_RESTART_REPLICA. That covers app subnet recovery, NNS recovery (failover nodes and same nodes) and subnet splitting. The subnet merging tool only stops nodes and never runsCMD_RESTART_REPLICA: the source subnet's node stays stopped, and the destination subnet's node stays stopped until the merged state is uploaded to it, which isn't a step of the tool yet.Root cause
The flaky test
sr_nns_failover_nodes_testfails because the replica on the upload node crash-loops after the recovery and never becomes healthy. It panics every ~10 s until the 600 s test timeout:Sequence of events, taken from the upload node's journal in the failed run of 2026-11-14:
UploadStateToChildNNSHostrsyncs the state asadminintonew_ic_state, so all its files are owned byadmin. It then runssystemctl stop ic-replica(09:28:39.634) and swapsnew_ic_statein asic_state.systemctl restart setup-permissions; systemctl start ic-replica; systemctl status ic-replica.setup-permissions.shruns withset -eandfind <dir> | xargs chown/chmodfor one directory after another. While it walked/var/lib/ic/crypto, the secret key store's temporary files vanished:ic_state, soic_statestayed owned byadmin. Since fix(ic-recovery): let setup-permissions setup permisisons #9281,setup-permissionsis the only thing that hands the uploaded state toic-replica, so this abort is fatal.;,ic-replicawas started anyway and the step reported success ("NNS recovery has finished"). The replica then crash-loops on theadmin-ownedic_state.Why the files vanished: only
ic-replicawas stopped. The crypto vault is a separate, socket-activated unit, so it kept running. When the replica disconnects, the vault drops the replica's queued requests, but a request that is already running on its thread pool runs to completion. Such a request ends with a crash-safe rewrite ofsks_data.pb: it creates a hard linksks_data.pb.old, writes and fsyncssks_data.pb.tmp, renames it oversks_data.pb, and then removes.old.findlisted both temporary names, and they were gone by the timechown/chmodran.The likely trigger is the forward-secure key update that the replica's DKG key manager starts on its first start on the recovery CUP. This is inferred from timing, because the vault logs that operation only at debug level. On the failover nodes this update moves the key from about epoch 1 to epoch 8, which is expensive and ends with a key store write. In the failed runs, the tool stopped the replica 1.7 s and 1.9 s after that work started, and the write landed about 0.4 s after the stop, while
setup-permissionswas walking/var/lib/ic/crypto. In the passing retries, the stop came 3.8 s and 13.8 s after, when the write was already done.WaitForCUPpolls every 15 s, so this gap is effectively random, which fits a flake rate of about 1%. The fix doesn't depend on which vault request does the write.Evidence:
sr_nns_failover_nodes_test_localon 2026-11-14 (master),sr_nns_failover_nodes_test_head_nns_localon 2026-11-09 (chore(ci): run bazel-test-all on Bazel Remote Execution @ Namespace #12052), andsr_nns_failover_nodes_test_head_nns_farm_colocateon 2026-10-29 (master). Older failures no longer have logs.systemctl restart setup-permissions, andsetup-permissions.servicefailed only 4 times. All 4 were on the upload node ofsr_nns_failover_nodes_test, right after this restart, withsks_data.pb.tmp/.oldvanishing. Three of them hit the race only in the finalrestoreconpass, afteric_statehad already been re-owned, so those tests passed.Why fix it in
ic-recoveryRemoterecoveries safe as soon as operators use the new binary, without any GuestOS change.Localmode uses the image's/opt/ic/bin/ic-recovery, so it gets the fix with new GuestOS versions. For the same reason, making systemd stop the vault together withic-replicathrough unit dependencies would only help nodes that already run a GuestOS version with the changed units.ic-replicais stopped first, and its processes are the vault's only clients. Stopping the vault after it guarantees that nothing in/var/lib/ic/cryptochanges whilesetup-permissionsruns. This restores the boot-time order:ic-crypto-csp.serviceis ordered aftergenerate-ic-config.service, which is ordered aftersetup-permissions.service. The socket is stopped as well, so that a connection can't socket-activate the vault in the meantime.rs/tests/consensus/node_registration_test_commonandrs/tests/node/ipv4_integration_test.rsalready stop and start the vault the same way, afteric-replica.&&: the vault is only stopped onceic-replicais down. A failed replica stop still fails the steps that runCMD_STOP_REPLICAas a command of its own, as before. InUploadCUPAndTarStep's script the remaining commands still run after a failed stop, also as before. The restart commands are joined by;: the node always ends with the vault and the replica started again, and the step's exit status doesn't change. Failing the step whensetup-permissionsfails is left out on purpose, because--skip-promptsretries a failing step forever.Note for crypto reviewers: a vault request that is still running for the replica that was just stopped is now killed right away, because the vault has no SIGTERM handler. This is the same as a crash or a reboot. Key store writes are crash-safe: a leftover
sks_data.pb.oldis cleaned up when the store is opened, and a leftoversks_data.pb.tmpis replaced at the next write.Verification
cargo check --all-targets --all-features -p ic-recovery -p ic-subnet-merging,cargo fmtand./ci/scripts/rust-lint.sh(in the dev container, exit 0) pass.//rs/recovery:recoveryand//rs/recovery/subnet_merging:subnet_mergingbuild, and the 6 tests within 2 reverse-dependency levels of the changed files pass.The rendered commands were checked with a fake
sudoinbashandsh.stop ic-replicafails,CMD_STOP_REPLICAdoesn't stop the vault and exits 1.restart setup-permissionsfails, the vault andic-replicaare still started.Current revision:
bazel test --test_output=errors --runs_per_test=3 --jobs=3 //rs/tests/consensus/subnet_recovery:sr_nns_failover_nodes_test_localpassed 3 of 3 runs.Stopped ic-replica.service→Stopped ic-crypto-csp.service→Closed ic-crypto-csp.socket→ (state swap) →Starting setup-permissions.service→Restoring SELinux security contexts in /var/lib/ic→Finished setup-permissions.service→Started ic-crypto-csp.service→Started ic-replica.service. There are nocannot accesserrors.StopReplica, also ranCMD_STOP_REPLICAon the broken NNS's download node, and the downloads from that node succeeded.Current revision, PR CI: for changes to
rs/recovery/src/*.rs,PULL_REQUEST_BAZEL_TARGETSselects the recovery system tests,long_testor not. This PR'sCI Main / Bazel Test Allrun on a90509a passed all 19 such targets:_localand_head_nns_localvariants ofsr_nns_failover_nodes_test;sr_app_no_upgrade_*tests, includingLocalmode insr_app_no_upgrade_locally_testand therecoveryuser insr_app_no_upgrade_provision_write_access_test;sr_app_same_nodes_*tests;subnet_splitting_test_local.I also ran
sr_app_no_upgrade_locally_test_locallocally, on top of master at 196bea3. On the node where the image'sic-recoveryran, the journal shows the same order as above.Current revision on top of master at 196bea3, covering what PR CI doesn't run:
sr_nns_failover_nodes_test_localpassed 2 of 2 runs. The loop was in the vault's cgroup before the step and was killed byCMD_STOP_REPLICAbefore the state swap.setup-permissionsthen completed with nocannot accesserrors.nr_broken_dfinity_node_farmpassed. It exercises the same-nodes NNS recovery'sStopReplica,UploadStateandUploadCUPAndRegistry(UploadCUPAndTarStep).setup-permissionsrestarts, and both finished.guestos-recovery-upgrader. They restartsetup-permissionsfromguestos-recovery-engine, which this PR doesn't change, and those restarts finished too.nr_locally_farmpassed. It runs the same steps through the image's/opt/ic/bin/ic-recoveryon the node itself (Localmode), with the same pattern on that node.subnet_cooling_down_test_localpassed. The subnet merging tool'sStopReplicaAtHaltingCheckpointStepstoppedic-replica, the vault and its socket on both download nodes, and the state downloads and validation that follow succeeded.First revision of this PR, which stopped the vault right before
setup-permissions, on Farm (_farm_colocate):WaitForCUPbefore any changed code ran, because the file-server VM servingregistry.tarnever became reachable. In about 820 earlier Farm runs of this test, that VM never stayed unreachable like this; it came up after at most 132 s.UploadStateToChildNNSHost, a test-only change (not part of this PR) starts a root loop on the upload node. The loop joinsic-crypto-csp.service's cgroup and keeps deleting and recreating 200 files in/var/lib/ic/crypto/churn, so it dies exactly when the vault is stopped.chown: cannot access '/var/lib/ic/crypto/churn/...',status=123, the replica crash-looping onFailed to init state layout ... PermissionDenied, the 600 s timeout).setup-permissionsran.Not run by PR CI:
subnet_cooling_down_test, the only system test of the subnet merging tool, is along_testthatPULL_REQUEST_BAZEL_TARGETSdoesn't select, so it only runs with theCI_ALL_BAZEL_TARGETSlabel.sr_app_failover_nodes_*,sr_app_large_no_upgrade_with_chain_keys_testand the nestednr_*NNS recovery tests (exceptnr_large_with_mainnet_state, which ismanual) aresystem_test_largewithbackend = "farm". They only run on Farm in release testing.Possible follow-ups (not in this PR)
setup-permissions.shtolerate paths that vanish while it runs, as defense in depth for new GuestOS images.ic-recoveryreport a failedsetup-permissions. Today a failure only shows up in the node's journal. Doing this needs a decision on how--skip-promptsshould handle retries.guestos-recovery-engine.shcallsperform_recoveryas anifcondition. That suspendsset -einside the function, so a failedsystemctl restart setup-permissionsthere goes unnoticed.setup_file_serverinsr_nns_failover_nodes_testdoesn't wait untilregistry.taris actually served. A file-server VM that never comes up therefore only shows up as the 600 s test timeout.This PR was created following the steps in
.claude/skills/fix-flaky-tests/SKILL.md.🤖 Generated with Claude Code