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
Two concurrent first saves to a new state file throw IOException "The file '…/state.json' already exists" #106
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:
Used a fresh directory, so the state file did not exist yet.
Created two Navigation<NavigationItem> instances sharing one JsonFilePersistenceProvider.
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.
Priority: Medium. A save fails with IOException and its data is lost, but only when two saves race before the state file exists, which happens on an app's very first run. Saves are fine once the file exists. Reproduced 16 times in 200.
Activity
matt-edmondson commentedon Nov 4, 2026
Triage
IOExceptionand its data is lost, but only when two saves race before the state file exists, which happens on an app's very first run. Saves are fine once the file exists. Reproduced 16 times in 200.JsonFilePersistenceProvider.SaveStateAsync(Navigation/Services/JsonFilePersistenceProvider.cs:77-84). Author of Save navigation state through a temporary file [patch] #105.File.Move(temp, path, overwrite: true)where it's available. Onnetstandard2.0, catch theIOExceptionand fall back toFile.Replace.Generated by Claude Code