perf(settings): write settings.json and wallets.json off the UI thread

Settings::save() and WalletIndex::save() ran the atomic write (temp
file, fsync, rename, directory fsync) on the UI thread: on every
preference change, and for wallets.json after every address-scan pass
whose wallet metadata changed, i.e. repeatedly during sync, when the
disk is busiest. Each took milliseconds to tens of milliseconds, and
seconds under heavy disk load.

The JSON is still built on the caller; the write is queued on an
App-owned CoalescingWriter, latest wins per file, one write at a time.

- After a fund- or security-critical setter (seed-migration stage,
  encryption pending, PIN enabled, active wallet file, wizard completed)
  the next save still waits until the file is on disk, so e.g. the
  sweep-submitted marker is durable before the sweep is broadcast. A
  failed durable save keeps the next one durable.
- Saves before the writer is attached (the upgrade re-save at startup),
  and in tests and tools, stay synchronous.
- main() flushes the queue (at most 3 s) before and after
  App::shutdown(), before the single-instance lock is released, so a
  relaunch never reads a stale settings.json.

A kill or power loss right after an ordinary save can now lose that
last preference change; the previous file stays intact. WalletIndex
saves report "queued" rather than "written" (no caller reads it). On a
wedged disk, exit can wait up to 2 x 3 s longer, inside the 15 s
watchdog.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-09 21:09:48 -05:00
co-authored by Claude Opus 5.5
parent 72054fe17f
commit 7649b1f8a3
8 changed files with 126 additions and 17 deletions
+6 -1
View File
@@ -8,6 +8,7 @@
#include <vector>
namespace dragonx {
namespace util { class CoalescingWriter; }
namespace data {
/**
@@ -41,9 +42,12 @@ struct WalletIndexEntry {
class WalletIndex {
public:
bool load();
bool save();
bool save(); // queued on the writer when one is attached (returns true once queued)
static std::string getDefaultPath();
/** @brief Route save() through a background writer (not owned; must outlive this). Null = synchronous. */
void setFileWriter(util::CoalescingWriter* writer) { writer_ = writer; }
const std::vector<WalletIndexEntry>& entries() const { return entries_; }
const std::vector<std::string>& extraFolders() const { return extra_folders_; }
@@ -64,6 +68,7 @@ private:
std::vector<WalletIndexEntry> entries_;
std::vector<std::string> extra_folders_;
std::string file_path_;
util::CoalescingWriter* writer_ = nullptr; // null -> synchronous writes
};
} // namespace data