You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
An empty .tmp left by a crash is restored ahead of a valid .bk, and the next save deletes the backup #339
When the main file is missing, ReadText tries the .tmp candidate before the .bk candidate (AppDataStorage/AppData.cs:189-197). TryRestoreFrom (AppData.cs:212-227) only checks whether the candidate exists, not whether it holds anything. A zero-length .tmp therefore wins over a valid .bk:
TryRestoreFrom(.tmp) copies the empty file over the main path, archives the .tmp, and returns true.
ReadText returns "".
LoadOrCreate treats "" as "no file yet" (AppData.cs:581-585) and calls Save() with defaults.
WriteText deletes .bk unconditionally (AppData.cs:155 and :165). The .bk is the only copy of the user's last good data, and it is never archived.
How the state arises
WriteText writes .tmp, copies main to .bk, deletes main, then moves .tmp into place (AppData.cs:152-165). Nothing flushes the .tmp contents to disk. A power loss after the delete but before the move can leave the .tmp directory entry in place with no data. The result is: main file missing, .tmp at 0 bytes, .bk intact.
Reproduction
This was confirmed with an MSTest run against MockFileSystem:
Setup: the main file is absent, <file>.tmp is empty, and <file>.bk contains {"Data":"precious"}.
Action: call LoadOrCreate().
Actual: it returns Data == "", and <file>.bk no longer exists. The user's data is gone and nothing records it.
#325 covers zero-length files after power loss, but its scenario and acceptance criteria are about an empty main file. This is a different code path: recovery in TryRestoreFrom promotes an empty candidate over a good backup, and the following save then destroys that backup.
Suggested fix
In TryRestoreFrom, treat a zero-length or whitespace-only candidate as unusable. Archive it (for example with a .empty or .corrupt suffix) and return false, so the .bk candidate is tried next.
Alternatively, or as well, have LoadOrCreate distinguish "restored but empty" from "no file", so a restored empty file is handled like unreadable content instead of triggering a save of defaults.
Acceptance criteria
With the main file missing, an empty .tmp and a valid .bk, LoadOrCreate() returns the backup's values.
No code path deletes a .bk whose content was never loaded or archived.
There is a regression test for the scenario above.
Priority: High. Recovery silently promotes an empty .tmp over a good .bk, then the next save deletes the .bk. The user's last good data is lost with no record. It takes a crash or power loss mid-write to set up, but recovery is exactly the moment this library is supposed to protect.
Area / suggested assignment: Crash-recovery and atomic-write path in AppData.cs (ReadText, TryRestoreFrom, WriteText, LoadOrCreate).
Notes: Two changes together would close it: reject zero-length or whitespace-only candidates in TryRestoreFrom, and stop WriteText from deleting a .bk it never loaded or archived. Consider flushing .tmp (FileOptions.WriteThrough or Flush(true)) before the delete/move, so this state can't arise in the first place.
Activity
matt-edmondson commentedon Oct 6, 2026
Triage
.tmpover a good.bk, then the next save deletes the.bk. The user's last good data is lost with no record. It takes a crash or power loss mid-write to set up, but recovery is exactly the moment this library is supposed to protect.AppData.cs(ReadText,TryRestoreFrom,WriteText,LoadOrCreate).TryRestoreFrom, and stopWriteTextfrom deleting a.bkit never loaded or archived. Consider flushing.tmp(FileOptions.WriteThroughorFlush(true)) before the delete/move, so this state can't arise in the first place.Generated by Claude Code