Repository navigation
fix(ic-recovery): let setup-permissions setup permisisons - #9281
Merged
pierugo-dfinity merged 3 commits intoMar 10, 2026
Merged
Conversation
pierugo-dfinity
marked this pull request as ready for review
March 10, 2026 09:46
eichhorl
approved these changes
Mar 10, 2026
andrewbattat
approved these changes
Mar 10, 2026
q-uint
pushed a commit
to Swiss-Subnet/ic
that referenced
this pull request
Nov 17, 2026
…es_test_local (dfinity#12116) `ic-recovery` now stops the crypto vault (`ic-crypto-csp.socket` and `ic-crypto-csp.service`) right after `ic-replica`. It starts the vault again only after it has re-run `setup-permissions`, which hands the replaced `ic_state` to `ic-replica`. So, as at boot, the vault is not running while `setup-permissions` runs. The commands are two shared constants in `ic_recovery::steps`: ```sh # CMD_STOP_REPLICA sudo systemctl stop ic-replica && sudo systemctl stop ic-crypto-csp.socket ic-crypto-csp.service; # ... replace ic_state, or the CUP and the registry local store ... # CMD_RESTART_REPLICA sudo systemctl restart setup-permissions; sudo systemctl start ic-crypto-csp.socket ic-crypto-csp.service; sudo systemctl start ic-replica; sudo systemctl status ic-replica; ``` Every step that stops the replica uses `CMD_STOP_REPLICA`: - `UploadStateAndRestartStep`, in both `Remote` and `Local` mode; - `UploadCUPAndTarStep`; - `StopReplicaStep`, the first step of both NNS recoveries; - the subnet merging tool's `StopReplicaAtHaltingCheckpointStep`. `UploadStateAndRestartStep` and `UploadCUPAndTarStep` then restart the node with `CMD_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 runs `CMD_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_test` fails 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: ``` Failed to init state layout: IoError { path: "/var/lib/ic/data/ic_state/page_deltas", message: "Failed to create directory", ... PermissionDenied } ``` Sequence of events, taken from the upload node's journal in the failed run of 2026-11-14: 1. The orchestrator starts the replica on the recovery CUP (09:28:37.8). 2. `UploadStateToChildNNSHost` rsyncs the state as `admin` into `new_ic_state`, so all its files are owned by `admin`. It then runs `systemctl stop ic-replica` (09:28:39.634) and swaps `new_ic_state` in as `ic_state`. 3. It runs `systemctl restart setup-permissions; systemctl start ic-replica; systemctl status ic-replica`. 4. `setup-permissions.sh` runs with `set -e` and `find <dir> | xargs chown/chmod` for one directory after another. While it walked `/var/lib/ic/crypto`, the secret key store's temporary files vanished: ``` chown: cannot access '/var/lib/ic/crypto/sks_data.pb.tmp': No such file or directory chown: cannot access '/var/lib/ic/crypto/sks_data.pb.old': No such file or directory setup-permissions.service: Main process exited, code=exited, status=123/n/a ``` The script aborted before it reached `ic_state`, so `ic_state` stayed owned by `admin`. Since dfinity#9281, `setup-permissions` is the only thing that hands the uploaded state to `ic-replica`, so this abort is fatal. 5. Because the commands are joined by `;`, `ic-replica` was started anyway and the step reported success ("NNS recovery has finished"). The replica then crash-loops on the `admin`-owned `ic_state`. Why the files vanished: only `ic-replica` was 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 of `sks_data.pb`: it creates a hard link `sks_data.pb.old`, writes and fsyncs `sks_data.pb.tmp`, renames it over `sks_data.pb`, and then removes `.old`. `find` listed both temporary names, and they were gone by the time `chown`/`chmod` ran. 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-permissions` was 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. `WaitForCUP` polls 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: - The same chain appears in three failures: `sr_nns_failover_nodes_test_local` on 2026-11-14 (master), `sr_nns_failover_nodes_test_head_nns_local` on 2026-11-09 (dfinity#12052), and `sr_nns_failover_nodes_test_head_nns_farm_colocate` on 2026-10-29 (master). Older failures no longer have logs. - ElasticSearch has VM logs since 2026-09-10. In that time, about 15.8k Farm testnets ran `systemctl restart setup-permissions`, and `setup-permissions.service` failed only 4 times. All 4 were on the upload node of `sr_nns_failover_nodes_test`, right after this restart, with `sks_data.pb.tmp`/`.old` vanishing. Three of them hit the race only in the final `restorecon` pass, after `ic_state` had already been re-owned, so those tests passed. ## Why fix it in `ic-recovery` - Recoveries run against nodes on GuestOS versions that are already deployed, and a broken subnet can't be upgraded before it is recovered. Fixing the tool makes `Remote` recoveries safe as soon as operators use the new binary, without any GuestOS change. `Local` mode 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 with `ic-replica` through unit dependencies would only help nodes that already run a GuestOS version with the changed units. - `ic-replica` is stopped first, and its processes are the vault's only clients. Stopping the vault after it guarantees that nothing in `/var/lib/ic/crypto` changes while `setup-permissions` runs. This restores the boot-time order: `ic-crypto-csp.service` is ordered after `generate-ic-config.service`, which is ordered after `setup-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_common` and `rs/tests/node/ipv4_integration_test.rs` already stop and start the vault the same way, after `ic-replica`. - The stop commands are joined by `&&`: the vault is only stopped once `ic-replica` is down. A failed replica stop still fails the steps that run `CMD_STOP_REPLICA` as a command of its own, as before. In `UploadCUPAndTarStep`'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 when `setup-permissions` fails is left out on purpose, because `--skip-prompts` retries 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.old` is cleaned up when the store is opened, and a leftover `sks_data.pb.tmp` is replaced at the next write. ## Verification - `cargo check --all-targets --all-features -p ic-recovery -p ic-subnet-merging`, `cargo fmt` and `./ci/scripts/rust-lint.sh` (in the dev container, exit 0) pass. `//rs/recovery:recovery` and `//rs/recovery/subnet_merging:subnet_merging` build, and the 6 tests within 2 reverse-dependency levels of the changed files pass. - The rendered commands were checked with a fake `sudo` in `bash` and `sh`. - If `stop ic-replica` fails, `CMD_STOP_REPLICA` doesn't stop the vault and exits 1. - If `restart setup-permissions` fails, the vault and `ic-replica` are 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_local` passed 3 of 3 runs. - In each run, the upload node's journal shows `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 no `cannot access` errors. - The test's first step, `StopReplica`, also ran `CMD_STOP_REPLICA` on 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_TARGETS` selects the recovery system tests, `long_test` or not. This PR's `CI Main / Bazel Test All` run on a90509a passed all 19 such targets: - the `_local` and `_head_nns_local` variants of `sr_nns_failover_nodes_test`; - the same two variants of the five `sr_app_no_upgrade_*` tests, including `Local` mode in `sr_app_no_upgrade_locally_test` and the `recovery` user in `sr_app_no_upgrade_provision_write_access_test`; - the same two variants of the three `sr_app_same_nodes_*` tests; - `subnet_splitting_test_local`. I also ran `sr_app_no_upgrade_locally_test_local` locally, on top of master at 196bea3. On the node where the image's `ic-recovery` ran, the journal shows the same order as above. - **Current revision on top of master at 196bea3, covering what PR CI doesn't run:** - **Forced-race A/B, fix arm:** with the loop described below, `sr_nns_failover_nodes_test_local` passed 2 of 2 runs. The loop was in the vault's cgroup before the step and was killed by `CMD_STOP_REPLICA` before the state swap. `setup-permissions` then completed with no `cannot access` errors. - **`nr_broken_dfinity_node_farm` passed.** It exercises the same-nodes NNS recovery's `StopReplica`, `UploadState` and `UploadCUPAndRegistry` (`UploadCUPAndTarStep`). - On the DFINITY-owned node these steps ran on, the journal shows the vault stopped and its socket closed before both `setup-permissions` restarts, and both finished. - The test recovers its other two nodes with `guestos-recovery-upgrader`. They restart `setup-permissions` from `guestos-recovery-engine`, which this PR doesn't change, and those restarts finished too. - **`nr_locally_farm` passed.** It runs the same steps through the image's `/opt/ic/bin/ic-recovery` on the node itself (`Local` mode), with the same pattern on that node. - **`subnet_cooling_down_test_local` passed.** The subnet merging tool's `StopReplicaAtHaltingCheckpointStep` stopped `ic-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`):** - **Plain runs:** 2 of 3 passed. The third timed out in `WaitForCUP` before any changed code ran, because the file-server VM serving `registry.tar` never 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. - **Forced-race A/B:** right before `UploadStateToChildNNSHost`, a test-only change (not part of this PR) starts a root loop on the upload node. The loop joins `ic-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. - master + loop: 2 of 2 runs failed exactly like the flaky runs (`chown: cannot access '/var/lib/ic/crypto/churn/...'`, `status=123`, the replica crash-looping on `Failed to init state layout ... PermissionDenied`, the 600 s timeout). - fix + loop: 3 of 3 runs passed, with the vault and the loop stopped before `setup-permissions` ran. - **Not run by PR CI:** - `subnet_cooling_down_test`, the only system test of the subnet merging tool, is a `long_test` that `PULL_REQUEST_BAZEL_TARGETS` doesn't select, so it only runs with the `CI_ALL_BAZEL_TARGETS` label. - `sr_app_failover_nodes_*`, `sr_app_large_no_upgrade_with_chain_keys_test` and the nested `nr_*` NNS recovery tests (except `nr_large_with_mainnet_state`, which is `manual`) are `system_test_large` with `backend = "farm"`. They only run on Farm in release testing. ## Possible follow-ups (not in this PR) - Make `setup-permissions.sh` tolerate paths that vanish while it runs, as defense in depth for new GuestOS images. - Let `ic-recovery` report a failed `setup-permissions`. Today a failure only shows up in the node's journal. Doing this needs a decision on how `--skip-prompts` should handle retries. - `guestos-recovery-engine.sh` calls `perform_recovery` as an `if` condition. That suspends `set -e` inside the function, so a failed `systemctl restart setup-permissions` there goes unnoticed. - `setup_file_server` in `sr_nns_failover_nodes_test` doesn't wait until `registry.tar` is 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](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Manually setting up permissions during the upload step takes time, even though we actually call
setup-permissionsafterwards anyways. Going through a few git blames, I understand that this service was introduced only afterwards, meaning we still also set the permissions manually in case the replicas did not have the service yet. This also explains the|| truewhen calling it. Now thatsetup-permissionsis not that recent anymore, we can remove those two work-arounds.