A local-first personal finance app
:electron: Window store app fix for temp global-store rewrites (edge case) (#8968)
Michael Clarkca9892b8527c
diff --git a/packages/desktop-electron/index.ts b/packages/desktop-electron/index.tsindex 2408f751b3..731d528f63 100644--- a/packages/desktop-electron/index.ts+++ b/packages/desktop-electron/index.ts@@ -160,16 +160,58 @@ if (isDev) {const getGlobalPrefsPath = () =>path.join(process.env.ACTUAL_DATA_DIR!, 'global-store.json');-async function loadGlobalPrefs() {- let state: GlobalPrefsJson = {};+// Complete copy of the store, written ahead of an in-place overwrite by the+// EXDEV fallback in saveGlobalPrefs (and by loot-core's asyncStorage), so an+// interrupted overwrite can be recovered from.+const getGlobalPrefsRecoveryPath = () => `${getGlobalPrefsPath()}.bak`;++function parseGlobalPrefs(contents: string): GlobalPrefsJson {+ const parsed: unknown = JSON.parse(contents);+ if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {+ throw new Error('Global preferences file is not a JSON object');+ }+ // The shape was checked above; the field values are trusted as written by+ // the app itself, the same way loot-core's asyncStorage treats them.+ return parsed as GlobalPrefsJson;+}++function loadGlobalPrefsRecovery(): GlobalPrefsJson | null {try {- state = JSON.parse(fs.readFileSync(getGlobalPrefsPath(), 'utf8'));+ return parseGlobalPrefs(+ fs.readFileSync(getGlobalPrefsRecoveryPath(), 'utf8'),+ );} catch {- logMessage('info', 'Could not load global state - using defaults');- state = {};+ return null;+ }+}++async function loadGlobalPrefs() {+ let contents: string;+ try {+ contents = fs.readFileSync(getGlobalPrefsPath(), 'utf8');+ } catch (error) {+ // A missing file is a fresh install: start from defaults, the same as+ // loot-core does, without consulting any leftover recovery copy.+ if ((error as NodeJS.ErrnoException).code !== 'ENOENT') {+ logMessage(+ 'error',+ `Could not read global state - using defaults: ${String(error)}`,+ );+ }+ return {};}- return state;+ try {+ return parseGlobalPrefs(contents);+ } catch {+ const recovered = loadGlobalPrefsRecovery();+ if (recovered) {+ logMessage('info', 'Loaded global state from its recovery copy');+ return recovered;+ }+ logMessage('info', 'Could not parse global state - using defaults');+ return {};+ }}// Like loadGlobalPrefs, but only a missing file falls back to defaults; a@@ -186,11 +228,16 @@ async function loadGlobalPrefsStrict(): Promise<GlobalPrefsJson> {throw error;}- const parsed: unknown = JSON.parse(contents);- if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {- throw new Error('Global preferences file is not a JSON object');+ try {+ return parseGlobalPrefs(contents);+ } catch (error) {+ const recovered = loadGlobalPrefsRecovery();+ if (recovered) {+ logMessage('info', 'Loaded global state from its recovery copy');+ return recovered;+ }+ throw error;}- return parsed as GlobalPrefsJson;}// Writes the global preferences file atomically (temp file + rename), the same@@ -200,11 +247,43 @@ async function saveGlobalPrefs(state: GlobalPrefsJson) {const globalPrefsPath = getGlobalPrefsPath();const temporaryPath = `${globalPrefsPath}.${process.pid}.main.tmp`;+ const contents = JSON.stringify(state);+try {- await writeFile(temporaryPath, JSON.stringify(state), 'utf8');+ await writeFile(temporaryPath, contents, 'utf8');await rename(temporaryPath, globalPrefsPath);+ // The atomic path never leaves a torn file, so any recovery copy from an+ // earlier in-place write is stale now; drop it.+ await rm(getGlobalPrefsRecoveryPath(), { force: true }).catch(+ () => undefined,+ );} catch (error) {await rm(temporaryPath, { force: true }).catch(() => undefined);++ if ((error as NodeJS.ErrnoException).code === 'EXDEV') {+ // Sandboxed installs (e.g. the Microsoft Store package) virtualise the+ // app data folder, so renaming into it fails as a cross-device move.+ // Write in place instead; losing atomicity beats failing outright.+ logMessage(+ 'info',+ `Could not atomically replace ${globalPrefsPath} (EXDEV); writing it in place instead`,+ );+ // Write-ahead copy: land the complete new contents in the recovery file+ // first, then overwrite in place. If the copy can't be written, refuse+ // to save rather than risk leaving a torn store as the only copy.+ try {+ await writeFile(getGlobalPrefsRecoveryPath(), contents, 'utf8');+ } catch (recoveryError) {+ logMessage(+ 'error',+ `Could not write the global preferences recovery copy; not overwriting the store: ${String(recoveryError)}`,+ );+ throw recoveryError;+ }+ await writeFile(globalPrefsPath, contents, 'utf8');+ return;+ }+throw error;}}diff --git a/packages/loot-core/src/platform/server/asyncStorage/index.electron.test.ts b/packages/loot-core/src/platform/server/asyncStorage/index.electron.test.tsindex df80aed00f..c8e1525aae 100644--- a/packages/loot-core/src/platform/server/asyncStorage/index.electron.test.ts+++ b/packages/loot-core/src/platform/server/asyncStorage/index.electron.test.ts@@ -72,6 +72,258 @@ describe('electron asyncStorage', () => {).toEqual([]);});+ it('falls back to writing in place when the rename crosses filesystems', async () => {+ // Sandboxed installs (e.g. the Microsoft Store package) virtualise the+ // app data folder, so the temp-file rename fails with EXDEV.+ const crossDeviceError = Object.assign(+ new Error('EXDEV: cross-device link not permitted'),+ { code: 'EXDEV' },+ );+ const renameSpy = vi+ .spyOn(fs.promises, 'rename')+ .mockRejectedValue(crossDeviceError);++ try {+ asyncStorage.init();+ await asyncStorage.setItem('language', 'en');+ await asyncStorage.setItem('theme', 'dark');++ expect(renameSpy).toHaveBeenCalled();+ expect(JSON.parse(fs.readFileSync(storePath(), 'utf8'))).toEqual({+ language: 'en',+ theme: 'dark',+ });+ expect(+ fs.readdirSync(dataDir).filter(name => name.endsWith('.tmp')),+ ).toEqual([]);+ } finally {+ renameSpy.mockRestore();+ }+ });++ it('writes the new store to the recovery copy ahead of the in-place write', async () => {+ const renameSpy = vi+ .spyOn(fs.promises, 'rename')+ .mockRejectedValue(Object.assign(new Error('EXDEV'), { code: 'EXDEV' }));++ try {+ asyncStorage.init();+ await asyncStorage.setItem('language', 'en');+ await asyncStorage.setItem('theme', 'dark');++ // The copy holds the complete latest store, not the previous one.+ const expected = { language: 'en', theme: 'dark' };+ expect(JSON.parse(fs.readFileSync(`${storePath()}.bak`, 'utf8'))).toEqual(+ expected,+ );+ expect(JSON.parse(fs.readFileSync(storePath(), 'utf8'))).toEqual(+ expected,+ );+ } finally {+ renameSpy.mockRestore();+ }+ });++ it('removes a stale recovery copy once an atomic write succeeds', async () => {+ fs.writeFileSync(`${storePath()}.bak`, JSON.stringify({ language: 'fr' }));++ asyncStorage.init();+ await asyncStorage.setItem('language', 'en');++ expect(fs.existsSync(`${storePath()}.bak`)).toBe(false);+ // The stale copy never leaked into the active store.+ expect(JSON.parse(fs.readFileSync(storePath(), 'utf8'))).toEqual({+ language: 'en',+ });+ });++ it('recovers from the backup copy when the store file is malformed', async () => {+ fs.writeFileSync(`${storePath()}.bak`, JSON.stringify({ language: 'en' }));+ // Simulates an in-place write interrupted part-way through.+ fs.writeFileSync(storePath(), '{"language": "en", "the');++ asyncStorage.init();++ expect(await asyncStorage.getItem('language')).toBe('en');+ // The damaged file is still preserved for inspection.+ expect(fs.existsSync(`${storePath()}.corrupt`)).toBe(true);+ });++ it('repairs the damaged store file after recovering', async () => {+ fs.writeFileSync(`${storePath()}.bak`, JSON.stringify({ language: 'en' }));+ fs.writeFileSync(storePath(), '{"language": "en", "the');++ asyncStorage.init();+ // Any queued write (including the repair) completes before this resolves.+ await asyncStorage.setItem('theme', 'dark');++ expect(JSON.parse(fs.readFileSync(storePath(), 'utf8'))).toEqual({+ language: 'en',+ theme: 'dark',+ });+ });++ it('survives the repair write failing after a recovery', async () => {+ fs.writeFileSync(`${storePath()}.bak`, JSON.stringify({ language: 'en' }));+ fs.writeFileSync(storePath(), '{"language": "en", "the');+ const renameSpy = vi+ .spyOn(fs.promises, 'rename')+ .mockRejectedValue(Object.assign(new Error('EXDEV'), { code: 'EXDEV' }));+ // The write-ahead copy can't be written, so the repair is refused.+ const realWriteFile = fs.promises.writeFile;+ const writeSpy = vi+ .spyOn(fs.promises, 'writeFile')+ .mockImplementation((target, ...rest) => {+ if (target === `${storePath()}.bak`) {+ return Promise.reject(+ Object.assign(new Error('ENOSPC'), { code: 'ENOSPC' }),+ );+ }+ return realWriteFile(target, ...rest);+ });++ // Observe rejections nobody handled: the repair write is fire-and-forget,+ // so a leak here would only ever show up as a process-level warning.+ const unhandledRejections: unknown[] = [];+ const onUnhandledRejection = (reason: unknown) => {+ unhandledRejections.push(reason);+ };+ process.on('unhandledRejection', onUnhandledRejection);++ try {+ asyncStorage.init();+ // The repair write does real filesystem work before it reaches the+ // failing recovery-copy write, so wait for that call rather than a+ // fixed number of ticks, then let the rejection propagate.+ await vi.waitFor(() => {+ expect(writeSpy).toHaveBeenCalledWith(+ `${storePath()}.bak`,+ expect.anything(),+ 'utf8',+ );+ });+ await new Promise(resolve => setImmediate(resolve));+ await new Promise(resolve => setImmediate(resolve));++ expect(unhandledRejections).toEqual([]);+ // The recovered store is still the live one in memory.+ expect(await asyncStorage.getItem('language')).toBe('en');+ } finally {+ process.off('unhandledRejection', onUnhandledRejection);+ writeSpy.mockRestore();+ renameSpy.mockRestore();+ }+ });++ it('ignores a malformed backup copy and falls back to defaults', async () => {+ fs.writeFileSync(`${storePath()}.bak`, 'also broken');+ fs.writeFileSync(storePath(), 'broken');++ asyncStorage.init();++ expect(await asyncStorage.getItem('language')).toBeUndefined();+ });++ it('keeps the recovery copy intact when the in-place write itself fails', async () => {+ const renameSpy = vi+ .spyOn(fs.promises, 'rename')+ .mockRejectedValue(Object.assign(new Error('EXDEV'), { code: 'EXDEV' }));++ try {+ asyncStorage.init();+ await asyncStorage.setItem('language', 'en');++ // Make only the in-place overwrite of the store fail (the temp file and+ // the recovery copy are written first and must still succeed).+ const realWriteFile = fs.promises.writeFile;+ const writeSpy = vi+ .spyOn(fs.promises, 'writeFile')+ .mockImplementation((target, ...rest) => {+ if (target === storePath()) {+ return Promise.reject(+ Object.assign(new Error('EIO'), { code: 'EIO' }),+ );+ }+ return realWriteFile(target, ...rest);+ });++ try {+ await expect(asyncStorage.setItem('theme', 'dark')).rejects.toThrow(+ 'EIO',+ );+ } finally {+ writeSpy.mockRestore();+ }++ // The recovery copy already holds the complete new state, so nothing is+ // lost even though the active file was never updated.+ expect(JSON.parse(fs.readFileSync(`${storePath()}.bak`, 'utf8'))).toEqual(+ { language: 'en', theme: 'dark' },+ );+ expect(JSON.parse(fs.readFileSync(storePath(), 'utf8'))).toEqual({+ language: 'en',+ });+ } finally {+ renameSpy.mockRestore();+ }+ });++ it('leaves the active file untouched when the recovery copy cannot be written', async () => {+ const renameSpy = vi+ .spyOn(fs.promises, 'rename')+ .mockRejectedValue(Object.assign(new Error('EXDEV'), { code: 'EXDEV' }));++ try {+ asyncStorage.init();+ await asyncStorage.setItem('language', 'en');+ const before = fs.readFileSync(storePath(), 'utf8');++ // Make only the recovery-copy write fail.+ const realWriteFile = fs.promises.writeFile;+ const writeSpy = vi+ .spyOn(fs.promises, 'writeFile')+ .mockImplementation((target, ...rest) => {+ if (target === `${storePath()}.bak`) {+ return Promise.reject(+ Object.assign(new Error('ENOSPC'), { code: 'ENOSPC' }),+ );+ }+ return realWriteFile(target, ...rest);+ });++ try {+ await expect(asyncStorage.setItem('theme', 'dark')).rejects.toThrow(+ 'ENOSPC',+ );+ } finally {+ writeSpy.mockRestore();+ }++ // The save was refused, so the store on disk is exactly as it was.+ expect(fs.readFileSync(storePath(), 'utf8')).toBe(before);+ } finally {+ renameSpy.mockRestore();+ }+ });++ it('still rejects when the rename fails for another reason', async () => {+ const renameSpy = vi+ .spyOn(fs.promises, 'rename')+ .mockRejectedValue(+ Object.assign(new Error('EACCES'), { code: 'EACCES' }),+ );++ try {+ asyncStorage.init();+ await expect(asyncStorage.setItem('language', 'en')).rejects.toThrow(+ 'EACCES',+ );+ expect(fs.existsSync(storePath())).toBe(false);+ } finally {+ renameSpy.mockRestore();+ }+ });+it('starts empty without a backup when no store file exists', () => {asyncStorage.init();diff --git a/packages/loot-core/src/platform/server/asyncStorage/index.electron.ts b/packages/loot-core/src/platform/server/asyncStorage/index.electron.tsindex d8ce8322dd..02e0b4b68c 100644--- a/packages/loot-core/src/platform/server/asyncStorage/index.electron.ts+++ b/packages/loot-core/src/platform/server/asyncStorage/index.electron.ts@@ -21,16 +21,53 @@ let writeCounter = 0;let pendingSave: Promise<void> = Promise.resolve();export const init: T.Init = function ({ persist = true } = {}) {+ persisted = persist;+if (persist) {- store = loadStore(getStorePath());+ const loaded = loadStore(getStorePath());+ store = loaded.store;+ if (loaded.recovered) {+ // The active file is damaged; rewrite it from the recovered state now+ // rather than leaving it broken until the next preference change. A+ // failure here is already logged by writeStore and must not take the+ // process down: the recovered store is still valid in memory.+ _saveStore().catch(() => undefined);+ }} else {store = {};}+};- persisted = persist;+// Sibling file holding a complete copy of the store. Only written by the+// non-atomic EXDEV fallback in writeStore, ahead of overwriting the store in+// place, so an interrupted overwrite can be recovered from. Cleared again by+// the atomic path, which never leaves a torn file behind.+const getRecoveryPath = (storePath: string) => `${storePath}.bak`;++function parseStore(contents: string): GlobalPrefsJson {+ const parsed = JSON.parse(contents);+ if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {+ throw new Error('Global preferences are not a JSON object');+ }+ return parsed;+}++function loadRecoveryStore(storePath: string): GlobalPrefsJson | null {+ const recoveryPath = getRecoveryPath(storePath);+ try {+ return parseStore(fs.readFileSync(recoveryPath, 'utf8'));+ } catch {+ return null;+ }+}++type LoadedStore = {+ store: GlobalPrefsJson;+ // True when the active file was unusable and the recovery copy was used.+ recovered: boolean;};-function loadStore(storePath: string): GlobalPrefsJson {+function loadStore(storePath: string): LoadedStore {let contents: string;try {contents = fs.readFileSync(storePath, 'utf8');@@ -40,19 +77,11 @@ function loadStore(storePath: string): GlobalPrefsJson {if (err?.code !== 'ENOENT') {logger.error('Could not read global preferences, using defaults', err);}- return {};+ return { store: {}, recovered: false };}try {- const parsed = JSON.parse(contents);- if (- parsed === null ||- typeof parsed !== 'object' ||- Array.isArray(parsed)- ) {- throw new Error('Global preferences are not a JSON object');- }- return parsed;+ return { store: parseStore(contents), recovered: false };} catch (err) {// The file exists but isn't a usable preferences object - either invalid// JSON (most likely truncated by an interrupted write, e.g. the process was@@ -63,16 +92,28 @@ function loadStore(storePath: string): GlobalPrefsJson {try {fs.writeFileSync(backupPath, contents, 'utf8');logger.error(- `Could not parse global preferences at ${storePath}; backed up the corrupt file to ${backupPath} and started with defaults`,+ `Could not parse global preferences at ${storePath}; backed up the corrupt file to ${backupPath}`,err,);} catch (backupErr) {logger.error(- `Could not parse global preferences at ${storePath}, and failed to back up the corrupt file; starting with defaults`,+ `Could not parse global preferences at ${storePath}, and failed to back up the corrupt file`,backupErr,);}- return {};++ // An in-place write (EXDEV fallback) may have been interrupted; it wrote+ // the complete new state to the recovery copy first, so prefer that over+ // starting from scratch.+ const recovered = loadRecoveryStore(storePath);+ if (recovered) {+ logger.warn(+ `Recovered global preferences from ${getRecoveryPath(storePath)}`,+ );+ return { store: recovered, recovered: true };+ }+ logger.error('No usable global preferences found; starting with defaults');+ return { store: {}, recovered: false };}}@@ -97,15 +138,55 @@ async function writeStore(): Promise<void> {// mid-write (such as during an app update).const tmpPath = `${storePath}.${process.pid}.${writeCounter++}.tmp`;+ const contents = JSON.stringify(store);+try {- await fs.promises.writeFile(tmpPath, JSON.stringify(store), 'utf8');+ await fs.promises.writeFile(tmpPath, contents, 'utf8');await fs.promises.rename(tmpPath, storePath);+ // The atomic path never leaves a torn file, so a recovery copy from an+ // earlier in-place write is now stale; drop it so it can't resurrect old+ // preferences if the store is ever damaged some other way.+ await fs.promises+ .rm(getRecoveryPath(storePath), { force: true })+ .catch(() => undefined);} catch (err) {// Best-effort cleanup of the temp file; ignore failures (it may never have// been created).try {await fs.promises.rm(tmpPath, { force: true });} catch {}++ if (err?.code === 'EXDEV') {+ // Some sandboxed installs (e.g. the Microsoft Store / MSIX package on+ // Windows) virtualise the app data folder so that renaming into it+ // counts as crossing filesystems. Fall back to writing the file in+ // place: not atomic, but far better than never being able to save+ // preferences at all.+ logger.warn(+ `Could not atomically replace ${storePath} (EXDEV); writing it in place instead`,+ );+ // Write-ahead copy: land the complete new contents in the recovery file+ // first, then overwrite the store in place. If the in-place write is+ // interrupted, loadStore recovers the new state from the copy. If the+ // copy itself can't be written, refuse to save rather than risk leaving+ // a torn store as the only copy.+ try {+ await fs.promises.writeFile(+ getRecoveryPath(storePath),+ contents,+ 'utf8',+ );+ } catch (recoveryErr) {+ logger.error(+ 'Could not write the global preferences recovery copy; not overwriting the store',+ recoveryErr,+ );+ throw recoveryErr;+ }+ await fs.promises.writeFile(storePath, contents, 'utf8');+ return;+ }+throw err;}}diff --git a/upcoming-release-notes/fix-store-install-global-prefs.md b/upcoming-release-notes/fix-store-install-global-prefs.mdnew file mode 100644index 0000000000..d0048d60b1--- /dev/null+++ b/upcoming-release-notes/fix-store-install-global-prefs.md@@ -0,0 +1,6 @@+---+category: Bugfix+authors: [MikesGlitch]+---++Fix the desktop app getting stuck on "Loading global preferences" when installed from the Microsoft Store