Skip to content

friglet-ipc: replace the scan key atomically (SNB-683) - #59

Closed
setavenger wants to merge 2 commits into
integrationfrom
snb-683-atomic-scan-key
Closed

setavenger wants to merge 2 commits into
integrationfrom
snb-683-atomic-scan-key

Conversation

@setavenger

Copy link
Copy Markdown
Owner

Stacked on #55; review only the commits after it.

Fixes SNB-683 (Bug). Part of the friglet cleanup (SNB-661).

The bug

friglet_ipc::write_key_file(path, hex, overwrite = true) opened scan.key with truncate and then wrote the key. A crash between the two left an empty key file, and the daemon would not start ("key file … is empty") until the key was entered again. The callers are the daemon's SetScanKey / ApplySettings, the descriptor-key migration at startup and the tray's first-run setup.

The fix (minimal)

overwrite = true now replaces the key the way write_config_toml already writes the config:

  1. Write <name>.tmp in the same directory, 0600 from creation (and enforced again, in case a crash left an old temp file).
  2. sync_all, then rename it over the key file.

The format (<hex>\n) and the 0600 mode are unchanged. overwrite = false (first key) is unchanged: create_new, fails if the file exists.

One edge-case difference, the same as for the config file: a key file that is a symlink is now replaced by a regular file instead of having its target rewritten.

Test

replacing_the_key_file_never_exposes_an_empty_file (Unix): a reader thread rereads the key file while it is replaced 200 times. It may only see the old key or the new one. Afterwards the mode is still 0600 and no .tmp file is left. The test applied to the old write_key_file failed 13 of 13 runs (a reader saw ""); with the fix it passes (0.5 s).

Verification

  • cargo check --workspace --all-targets --all-features: 0 warnings
  • cargo clippy --no-deps -p friglet -p friglet-ipc -p friglet-tray --all-targets -- -D warnings: clean
  • cargo fmt -p friglet -p friglet-ipc -p friglet-tray -- --check: clean
  • cargo test --workspace: 255 passed, 1 ignored, 0 failed (254 + the new test)
  • Not run: a Windows cross-check (the x86_64-pc-windows-gnu std is not installed here). The Windows path uses the same std::fs::rename as write_config_toml.

setavenger and others added 2 commits October 3, 2026 09:09
friglet-ipc items only its own module uses are private now:
write_message, read_message, SpDescriptor::scan_pubkey,
descriptor_checksum, the log-file constants and RotatingLog::with_limit.
No other crate in the workspace uses them, and neither does friglet-ui
(it uses Request/Response/StatusInfo, Client, listen, accept,
read_config_toml and DaemonConfig).

Comments: no frigletctl tool exists; state_file documents the per-wallet
file name; OkWithNote's example is what the daemon sends today;
oracle_connected describes the background poll, not a cache TTL. The
default oracle_url uses the same constant as hosted_oracle_url("bitcoin")
instead of repeating the URL. The roundtrip test sets scan_health twice;
blindbit-cli has four comments that restate the next line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
write_key_file(.., overwrite = true) truncated scan.key and then wrote
it. A crash between the two left an empty key file, and the daemon would
not start until the key was entered again. It now writes
`<name>.tmp` in the same directory (0600 from creation, enforced again
for a leftover temp file), syncs it and renames it over the key file,
like write_config_toml. The file format ("<hex>\n") and the 0600 mode
are unchanged; creating a new key (overwrite = false) still fails when
the file exists.

New test: a reader thread rereads the key while it is replaced 200
times and must only ever see the old or the new key. On the old code it
sees an empty file (failed 13 of 13 runs).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@setavenger

Copy link
Copy Markdown
Owner Author

Merged into integration through #69 on 2026-10-11 (the user live-tested the combined stack first). GitHub's stacked-PR merge rebased the stack, so this PR's commits landed with new IDs and GitHub can't mark it merged itself. The code is identical: integration now has the same tree as the tested branch friglet-deslop-live-test (08bf11d), which contains this PR's head. Closing as merged via #69.

@setavenger setavenger closed this Oct 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant