Skip to content

An empty .tmp left by a crash is restored ahead of a valid .bk, and the next save deletes the backup #339

Closed

Description

@matt-edmondson

What's wrong

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:

  1. TryRestoreFrom(.tmp) copies the empty file over the main path, archives the .tmp, and returns true.
  2. ReadText returns "".
  3. LoadOrCreate treats "" as "no file yet" (AppData.cs:581-585) and calls Save() with defaults.
  4. 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.
  • Expected: it returns Data == "precious".

Relation to #325

#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.

Activity

  1. added
    bug
    on Oct 6, 2026
  2. matt-edmondson commented on Oct 6, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    • Category: Bug
    • 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).
    • Duplicates / related: Not a duplicate. Related to A power loss or OS crash just after a save can leave a zero-length settings file, which LoadOrCreate then silently overwrites with defaults #325 (empty main file after power loss), which covers a different code path, as the issue explains.
    • In progress: No open PR covers it.
    • 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.

    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions