Skip to content

friglet-tray: edit config.toml when the daemon is down (#78) - #83

Open
setavenger wants to merge 5 commits into
integrationfrom
fix-78-start-at-tip-offline
Open

setavenger wants to merge 5 commits into
integrationfrom
fix-78-start-at-tip-offline

Conversation

@setavenger

@setavenger setavenger commented Oct 30, 2026 •

Copy link
Copy Markdown
Owner

Fixes #78 on the tray side: the issue's "Expected", and fix 2 in the follow-up comment.

Problem. Outside first-run setup, the Settings form only loads and saves through the daemon's control socket. If a saved config makes the daemon exit at startup (start_at_tip while the oracle is down, an unresolvable P2P host, …), the form that could fix it stays disabled.

Change. When the tray's own daemon has crashed (Status: "crashed — restarting"), the form switches to file mode:

  • It loads config.toml through get_config_file. A descriptor key fills in the spend key and bh= birthday, as the daemon applies them. A file that can't be read is shown as the reason and retried on every poll.
  • Save goes through save_config_file. The tray checks the settings itself, including secp256k1 checks on the keys. The scan key is optional: a descriptor in the file supplies it, otherwise the key file is kept. Under the lifecycle lock it refuses if a daemon answers, stops our own child if it isn't answering, keeps a birthday a restart attempt recorded meanwhile, and writes the file. It then starts the daemon the way Start daemon does: it clears a user stop and resets the backoff.
  • The form stays in file mode through restart attempts and switches back once the daemon answers. Reload re-reads the file; if the file can't be read, the form is disabled until it can, so Save never writes an old copy over it.
  • External daemons never enter file mode, because they may use another config file.

Verified

  • cargo test -p friglet-tray: 70 unit and 7 e2e tests pass. Clippy -D warnings and fmt are clean.
  • Real tray and daemon under Xvfb with isolated XDG dirs, with screenshots taken:
    • start_at_tip = true with an unreachable oracle reproduced the crash loop. The form came up editable and filled from the file. Choosing a block height and saving wrote start_height, dropped start_at_tip and respawned the daemon.
    • An unresolvable p2p_node_addr made the daemon exit at startup. Fixing it in the form gave "Saved — daemon started." and the form went back to loading from the daemon.
    • An unparseable config.toml showed the parse error with the form disabled. After the file was fixed by hand, the form loaded by itself.
    • Choosing "New wallet" on a file with a fixed height saved; the daemon recorded the tip and started.
    • Breaking config.toml by hand while the form was loaded, then pressing Reload, disabled the form and showed the parse error. Fixing the file loaded the form again by itself.
  • Four /code-review passes; triage is in the PR comments.

Not in this PR

  • The issue's fix 1, on the daemon side: with start_at_tip, the daemon still exits when the oracle can't answer GetInfo. It needs start_height before it brings up the scanner, Electrum and the control socket.
  • Also seen while testing: if the oracle refuses TCP connections (rather than answering with a 502), the daemon exits with transport error even when start_height is fixed.

setavenger and others added 3 commits October 30, 2026 07:55
Outside first-run setup the Settings form only loaded and saved through
the daemon's control socket. A saved config that makes the daemon exit
at startup (start_at_tip while the oracle is down, an unresolvable P2P
host, ...) therefore locked the form that could fix it.

When the daemon has crashed or is not running, the form now loads the
config file (get_config_file) and Save writes it through the tray
(save_config_file: setup's validation, scan key optional so the key
file is kept), then starts the daemon. The form stays in this mode
through restart attempts until the daemon answers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- A `descriptor` in the config file supplies the keys when nothing new
  is entered (the save drops the key, and the daemon may have exited
  before copying its scan key), so a stale key file can no longer be
  paired with the descriptor's spend key; the network check applies.
- Scan and spend keys are checked as secp256k1 keys before a save can
  replace a working key file.
- Save starts the daemon like Start daemon: clears a user stop and
  resets the restart backoff.
- File mode is entered only for the tray's own crashed daemon
  ("restarting"); an external daemon may use another config file.
- A config file that cannot be read is shown as the reason and retried
  each poll, so a hand fix is picked up without restarting the tray.
- "New wallet" kept as loaded adopts a start_height a restart attempt
  recorded meanwhile, instead of moving the birthday to a later tip.
- Spawn-failure messages survive the switch into file mode; a daemon
  save stops waiting once the restarted daemon crashed; a late daemon
  config load no longer overwrites the file contents.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The "keep a recorded birthday" check moves into save_config_file,
  under the lifecycle lock, so it cannot race a restart attempt that
  records start_height; the form only says whether "new wallet" is
  unchanged for the same wallet and network. After a save whose start
  failed, the baseline is what was written, not a stale load.
- An empty or unparseable descriptor in the file no longer blocks
  saves (the save drops it); a file descriptor whose spend key differs
  from the one shown is refused instead of silently swapped in.
- A tray-owned child that does not answer is stopped before the file
  is written, so it cannot record a birthday into the new file.
- get_config_file is async and names env-only configs when there is
  no file; docs match the "restarting"-only entry.

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

Copy link
Copy Markdown
Owner Author

Triage of two /code-review passes. Fixed in e7218ed and 4a35a08; the rest are deliberately left out.

Fixed

  • The file's descriptor was dropped on save while a stale key file was kept, which could pair the wrong scan key with its spend key. The descriptor now supplies the keys. An empty or invalid descriptor no longer blocks saves, and a descriptor spend key that differs from the one shown is refused.
  • Scan and spend keys are now checked as secp256k1 keys before a save can replace a working key file.
  • Save now starts the daemon like Start daemon: it clears a user stop and resets the restart backoff.
  • File mode is entered only for the tray's own crashed daemon, because an external daemon may use another config file.
  • A config file that can't be read is now retried every poll, and env-only setups are named in the message.
  • "New wallet" no longer moves a birthday that a restart attempt recorded meanwhile. The check runs under the lifecycle lock and only applies to the same wallet and network, and the baseline after a failed start is what was written.
  • A tray-owned child that isn't answering is stopped before the file is written.
  • Spawn-failure messages now survive the switch into file mode. A daemon-backed save stops waiting once the daemon has crashed, and a late get_config no longer overwrites the file contents in the form.
  • Docs now match the behaviour.

Not changed

  • Key file written before the config, so a failed config write could leave a mismatch: this needs a failure between two local writes (disk full, permissions), and either order leaves a window. It's left as is.
  • A daemon-mode save reports "exited with the new settings" on the first crash even if the next restart succeeds: the message is accurate, and it is cleared when the form reloads from the daemon.
  • Making file-mode eligibility a backend flag rather than the restarting view state, and having save_config_file re-check it: this is worth doing, but save_config_file has one caller and that caller is already gated. Follow-up.
  • Duplicated save flows in JS and Rust, one mode variable instead of booleans, and parsing the file once: these are refactors. Follow-up.
  • Env-only configs are still not editable in file mode; the message now says to change the environment variables.
  • The root cause, a daemon that exits before its control socket comes up, is not addressed. See "Not in this PR" in the description.

- The "unresolved birthday at load" flag is always a bool: with
  start_at_tip skipped when false, the old expression could be
  undefined, and Tauri rejected every such "New wallet" save.
- save_config_file decides whether to keep a recorded birthday itself:
  only for the same wallet and network as the file on disk, and only
  when the form loaded an unresolved "new wallet" birthday. A re-pasted
  descriptor of the same wallet no longer drops it, and an explicit
  height in a hand-edited file is no longer adopted over "New wallet".
- After a save whose start failed, the form reloads the file, so a
  birthday the daemon recorded before exiting is shown and kept.
- A missing key file fails the save before our child is stopped; the
  exited child is reaped (and logged) first; the child teardown is one
  helper shared with attach_and_record.
- The network-mismatch test asserts the network error again.

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

Copy link
Copy Markdown
Owner Author

Third /code-review pass, on 4a35a08. Fixed in the latest commit.

Fixed

  • "New wallet" saves failed whenever the loaded file had no start_at_tip = true. start_at_tip isn't serialized when false, so the JS flag was undefined, and Tauri rejected the call with "missing required key". The flag is now always a bool. Checked in the real tray: choosing "New wallet" on a file with a fixed height saves, and the daemon records the tip and starts.
  • save_config_file now decides itself whether to keep a recorded birthday, checking that the file on disk has the same wallet and network. The form only says whether it loaded an unresolved "new wallet" birthday. As a result:
    • re-pasting the same wallet's descriptor no longer loses a recorded birthday;
    • a hand-edited explicit height is no longer adopted over "New wallet";
    • another wallet or network never inherits one.
  • After a save whose daemon start failed, the form reloads the file, so a birthday the daemon recorded before exiting is shown and kept.
  • A missing key file now fails the save before our child is stopped. Before, the child could be killed and then left dead with no restart scheduled.
  • An already-exited child is reaped and logged before the kill. The kill sequence is now one helper, shared with attach_and_record.
  • The network-mismatch test now asserts the network error again; it had been passing on the spend-key error.

Not changed

  • Env-only setups where the daemon created a minimal config.toml (it holds only start_height) still load that file in file mode. Tray users with env-only configs are rare. Follow-up.

A Reload that cannot read config.toml (e.g. a hand edit left it
broken) kept the old copy and an enabled form, so Save wrote that
copy over the file. Drop it: the form is disabled with the error and
loads by itself once the file can be read again.

Drop the second key-file check in write_config_file; save_config_file
checks it before stopping the child.

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

Copy link
Copy Markdown
Owner Author

Review 4 (/code-review on 7e72fb5): 9 findings.

Fixed in 6ef6d14:

  • A Reload that couldn't read config.toml kept the old copy and left the form enabled, so Save wrote that copy over a file someone had edited (and broken) by hand. Now the form is disabled and shows the error, and it loads by itself once the file can be read again. Checked in the real tray with screenshots.
  • ensure_key_file ran twice per save. The call inside write_config_file is gone; save_config_file still checks before it stops the child.

Not changed:

  • A hand edit made while the form is loaded, followed by Save without Reload: Save writes the form's copy. The daemon-mode save (ApplySettings) already rewrites the whole file the same way. A conflict check would have to tell a birthday the daemon recorded apart from a hand edit.
  • FRIGLET_* env vars that override the file: the form shows the file, and a save can't fix a value that comes from the env. It isn't silent: the respawned daemon's error shows as "Saved, but the daemon failed to start: …". Tray users rarely set these. Follow-up, together with the env-only configs from review 2.
  • Unsaved edits in the daemon-backed form are replaced by the file when the daemon crashes. This needs the daemon to crash while you type. Keeping the edits would need a dirty-form baseline, which interacts with the birthday protection (tipUnresolvedAtLoad).
  • An adopted daemon (no child handle) that is wedged but alive: the save spawns a new daemon next to it. The supervisor already does the same on its next tick once it presumes the daemon dead, so this isn't new.
  • Refactors, all follow-ups as after reviews 2 and 3: share apply_descriptor between daemon and tray through friglet-ipc; merge the form's changes into the on-disk TOML table instead of rewriting the file (this would make keep_recorded_birthday unnecessary); one helper for the descriptor and scan-key precedence, which now exists three times; parse the file once per operation.

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