Skip to content

Commit 44ffb65

Browse files
committed
Address ionite34 review feedback
- Add timestamp to .bak filenames (e.g. settings.json.20260408-020219.bak) to preserve original backup if corruption recurs - Add try-catch around File.Move in SaveSettings/SaveSettingsAsync to clean up orphaned .tmp files if the move fails due to file locks https://claude.ai/code/session_01Gej4ey7eUsbPRDqFWwxKyZ
1 parent 0e27444 commit 44ffb65

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

StabilityMatrix.Core/Services/SettingsManager.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,8 @@ private void BackupCorruptedFile(byte[] rawBytes)
568568
{
569569
try
570570
{
571-
var backupPath = SettingsFile + ".bak";
571+
var timestamp = DateTime.Now.ToString("yyyyMMdd-HHmmss");
572+
var backupPath = SettingsFile + $".{timestamp}.bak";
572573
File.WriteAllBytes(backupPath, rawBytes);
573574
logger.LogInformation("Backed up corrupted settings file to {BackupPath}", backupPath);
574575
}
@@ -614,7 +615,16 @@ protected virtual void SaveSettings(CancellationToken cancellationToken = defaul
614615
// Write to temp file then rename for atomic save
615616
var tempPath = SettingsFile + ".tmp";
616617
File.WriteAllBytes(tempPath, jsonBytes);
617-
File.Move(tempPath, SettingsFile, overwrite: true);
618+
try
619+
{
620+
File.Move(tempPath, SettingsFile, overwrite: true);
621+
}
622+
catch (Exception ex)
623+
{
624+
logger.LogWarning(ex, "Failed to move temp settings file, cleaning up");
625+
try { File.Delete(tempPath); } catch { /* best effort */ }
626+
throw;
627+
}
618628
}
619629
finally
620630
{
@@ -658,7 +668,16 @@ protected virtual async Task SaveSettingsAsync(CancellationToken cancellationTok
658668
// Write to temp file then rename for atomic save
659669
var tempPath = SettingsFile + ".tmp";
660670
await File.WriteAllBytesAsync(tempPath, jsonBytes, cancellationToken).ConfigureAwait(false);
661-
File.Move(tempPath, SettingsFile, overwrite: true);
671+
try
672+
{
673+
File.Move(tempPath, SettingsFile, overwrite: true);
674+
}
675+
catch (Exception ex)
676+
{
677+
logger.LogWarning(ex, "Failed to move temp settings file, cleaning up");
678+
try { File.Delete(tempPath); } catch { /* best effort */ }
679+
throw;
680+
}
662681
}
663682
finally
664683
{

0 commit comments

Comments
 (0)