Skip to content

Two concurrent first saves to a new state file throw IOException "The file '…/state.json' already exists" #106

Open

Description

@matt-edmondson

What's wrong

JsonFilePersistenceProvider.SaveStateAsync (since #105, d1639dc) writes each save to its own <file>.<guid>.tmp and then swaps it in:

// Navigation/Services/JsonFilePersistenceProvider.cs:77-84
if (File.Exists(_filePath))
{
    File.Replace(tempPath, _filePath, destinationBackupFileName: null);
}
else
{
    File.Move(tempPath, _filePath);
}

File.Move without overwrite throws when the destination exists. If two saves both see File.Exists == false and one of them moves its temp file into place first, the other's File.Move throws IOException: The file '…/state.json' already exists. The catch block deletes that save's temp file and rethrows, so the caller's SaveStateAsync fails even though nothing was wrong with its data.

Reproduction

This was checked on Linux with .NET 10. A scratch console app referencing the built ktsu.Navigation.dll did the following on each iteration:

  1. Used a fresh directory, so the state file did not exist yet.
  2. Created two Navigation<NavigationItem> instances sharing one JsonFilePersistenceProvider.
  3. Ran both SaveStateAsync() calls together: Task.WhenAll(Task.Run(a.SaveStateAsync), Task.Run(b.SaveStateAsync)).
  • Actual: 16 of 200 pairs threw System.IO.IOException: The file '/tmp/…/state.json' already exists.
  • Expected: both saves succeed, and the last writer wins, as it does when the file already exists (the File.Replace path).

Why it matters

Two overlapping saves are a normal setup, for example saving on navigation and again on shutdown, or two windows sharing one provider. The race only exists before any save has been written, so it hits the very first run of an app. Existing tests always save over an existing file or save one at a time, so none of them cover it. Before #105, File.WriteAllTextAsync did not fail this way.

Suggested fix

  • On targets that have it, use File.Move(tempPath, _filePath, overwrite: true). On netstandard2.0, catch the IOException from File.Move and fall back to File.Replace. Either way the swap is atomic whichever save gets there first.
  • Optionally, serialize the swap per path with a SemaphoreSlim, so an older snapshot cannot replace a newer one when the two saves finish out of order.
  • Add a test that runs two concurrent first saves against a non-existent path in a loop and asserts that neither throws and that the file deserializes.

Activity

  1. added
    bug
    on Nov 4, 2026
  2. matt-edmondson commented on Nov 4, 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

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions