Skip to content

A power loss or OS crash during SaveAsync can leave a zero-length profiles.json: temp files are renamed into place without being flushed to disk (gap in #157) #237

Open

Description

@matt-edmondson

What's wrong

WriteFileUnlockedAsync writes the temporary file with File.WriteAllTextAsync and then immediately moves it over the target:

private async Task WriteFileUnlockedAsync(string fileName, string contents)
{
string path = Path.Combine(_dataDirectory, fileName);
string tempPath = Path.Combine(_dataDirectory, $"{fileName}.{Guid.NewGuid():N}.tmp");
try
{
await File.WriteAllTextAsync(tempPath, contents).ConfigureAwait(false);
File.Move(tempPath, path, overwrite: true);
}
finally
{
if (File.Exists(tempPath))
{
File.Delete(tempPath);
}
}
}

The temp file's data is never flushed to stable storage before the rename. There is no FileStream.Flush(flushToDisk: true) and no FileOptions.WriteThrough. That covers a process crash, but not a power loss or kernel crash. On XFS, btrfs, ext4 with noauto_da_alloc, and other filesystems, the rename can reach the disk before the file's data does, which is the well-known zero-length-file-after-rename failure. #157 named "a crash or power loss" as the failure to prevent, and the class remarks promise the file is "never a truncated one".

If this happens to profiles.json, every profile is lost. The next load sees an empty file, treats it as corrupt and moves it aside, but the moved-aside copy is empty too, so nothing can be recovered.

Evidence

Tracing the file-sync and rename system calls during one SaveAsync:

strace -f -e trace=fsync,fdatasync,sync_file_range,rename,renameat,renameat2 app proc 1 $D
rename(".../commands.json.<guid>.tmp", ".../commands.json") = 0
rename(".../profiles.json.<guid>.tmp", ".../profiles.json") = 0
rename(".../active-profile.json.<guid>.tmp", ".../active-profile.json") = 0

There is no fsync or fdatasync before any of the renames. (Power loss itself wasn't simulated; the gap shows in the system-call trace.)

Suggested fix / acceptance criteria

  • Write the temp file through a FileStream and call Flush(flushToDisk: true) before disposing it, or open it with FileOptions.WriteThrough. Then do the File.Move.
  • On Unix, optionally fsync the containing directory after the rename so the rename itself is durable.
  • Check the fix with a strace, or with a test using an injected writer, showing the data flushed before the rename. The existing Saves_LeaveNoTemporaryFilesBehind test should still pass.

Activity

  1. added
    bug
    on Nov 8, 2026
  2. matt-edmondson commented on Nov 8, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions