fix(settings): a read of the archive's settings never undoes an edit saved meanwhile

Settings › Archive reads archive.json without the settings lease each time it
is shown and after a waited edit. On a slow share such a read could return
the file as it was before a template, proxy or thumbnail edit that was being
written, then put the old value back on the row and in this PC's defaults
while the archive kept the edit. ArchiveReadGate now drops a read that
overlapped an edit being written; the rows are read again at once, or when
the last edit being written ends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 19:00:45 +00:00
1 parent 93054ba5de
commit 59d49f16af
3 files changed
+167 -9

No files matched your search

+29 -9
View File
@@ -51,6 +51,9 @@ public sealed class SettingsViewModel : SectionViewModel
/// there: edits of the Archive rows made meanwhile wait for it (<see cref="ArchiveAtFolderAsync"/>).
/// </summary>
private Task<Archive?> _retarget = Task.FromResult<Archive?>(null);
/// <summary>Drops a read of the open archive's settings that may have missed an edit being written meanwhile.</summary>
private readonly ArchiveReadGate _archiveReads = new();
private bool _isDetectingFormats;
private IReadOnlyList<BackupOption> _backups = [];
private BackupOption? _selectedBackup;
@@ -923,15 +926,24 @@ public sealed class SettingsViewModel : SectionViewModel
Archive? archive = null;
try
{
var registered = await db.Archives.ListAsync();
archive = root is { Length: > 0 } ? CurrentArchive.At(registered, root) : null;
if (!IsArchiveFolder(root)) return archive;
_archive = archive;
if (archive is null) return null;
var settings = await Task.Run(() => archives.RefreshSettingsAsync(archive.Id))
ArchiveSettings settings;
while (true)
{
var read = _archiveReads.BeginRead();
var registered = await db.Archives.ListAsync();
archive = root is { Length: > 0 } ? CurrentArchive.At(registered, root) : null;
if (!IsArchiveFolder(root)) return archive;
_archive = archive;
if (archive is null) return null;
settings = await Task.Run(() => archives.RefreshSettingsAsync(archive.Id))
?? new ArchiveSettings(archive.Name, new LayoutTemplates { Folder = archive.FolderTemplate, File = archive.FileTemplate },
new ProxyPolicy { LongEdge = archive.ProxyLongEdge }, new ThumbnailPolicy { LongEdge = _s.ThumbnailLongEdge });
if (!IsArchiveFolder(root)) return archive;
if (!IsArchiveFolder(root)) return archive;
// A read that overlapped an edit being written may have missed it: it is not shown, and the rows are read
// again now, or once the last edit being written ends (ArchiveReadGate).
if (_archiveReads.IsCurrent(read, out var readAgain)) break;
if (!readAgain) return archive;
}
if (!FolderTemplate.IsDirty) FolderTemplate.Load(settings.Templates.Folder);
if (!FileTemplate.IsDirty) FileTemplate.Load(settings.Templates.File);
var changed = _s.FolderTemplate != settings.Templates.Folder || _s.FileTemplate != settings.Templates.File
@@ -984,6 +996,8 @@ public sealed class SettingsViewModel : SectionViewModel
retarget ??= _retarget;
var waited = !retarget.IsCompleted;
if (await ArchiveAtFolderAsync(retarget) is not { } archive || Shell.Services.Archives is not { } archives) return;
bool reload;
_archiveReads.BeginWrite();
try
{
var updated = await Task.Run(() => archives.UpdateSettingsAsync(archive.Id, edit));
@@ -991,15 +1005,21 @@ public sealed class SettingsViewModel : SectionViewModel
if (_archive?.Id == updated.Id) _archive = updated;
// The open showed the archive's values over the edit: unless another folder was chosen since, the rows (and
// this PC's defaults) take what it holds now.
if (waited && _retarget == retarget) await LoadArchiveAsync();
reload = waited && _retarget == retarget;
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or ArgumentException or InvalidDataException or NewerFormatException
or DatabaseException)
{
Log.Warning("settings", "The archive settings were not saved", ex);
Shell.Notify(Loc.L("Settings.Archive.NotSaved"), ArchiveActions.Cause(ex), isError: true);
await LoadArchiveAsync();
reload = true;
}
finally
{
// A read of the archive's settings this write overlapped was dropped: the rows are read again.
if (_archiveReads.EndWrite()) reload = true;
}
if (reload) await LoadArchiveAsync();
}
/// <summary>
@@ -0,0 +1,56 @@
namespace Titano.App.Settings;
/// <summary>
/// Orders Settings › Archive's reads of the open archive's settings (its archive.json and index row) against the edits
/// written into them. A read that overlapped a write may hold the file as it was before the edit (a share waking up
/// answers late, and reads take no settings lease): showing it would undo the edit on the rows and in this PC's defaults
/// while the archive keeps it. Such a read is dropped, and the rows are read again once no edit is being written: at
/// once when none is left (<see cref="IsCurrent"/>), else when the last one ends (<see cref="EndWrite"/>). Used from the
/// interface thread only.
/// </summary>
public sealed class ArchiveReadGate
{
/// <summary>The token of a read begun while an edit was being written: never current.</summary>
private const int Overlapped = -1;
private int _begun;
private int _writing;
private bool _readAfterWrites;
/// <summary>An edit starts being written into the archive.</summary>
public void BeginWrite()
{
_begun++;
_writing++;
}
/// <summary>
/// The edit was written, or failed. True when the rows must be read again now: a read it overlapped was dropped and
/// no other edit is being written.
/// </summary>
public bool EndWrite()
{
if (_writing == 0) throw new InvalidOperationException("No edit is being written.");
_writing--;
if (_writing > 0 || !_readAfterWrites) return false;
_readAfterWrites = false;
return true;
}
/// <summary>A read starts; its token tells <see cref="IsCurrent"/> which edits it may have missed.</summary>
public int BeginRead() => _writing > 0 ? Overlapped : _begun;
/// <summary>
/// Whether what a read begun with <paramref name="token"/> found may be shown: no edit was being written when it began
/// and none began since. When it may not, the rows are read again: now (<paramref name="readAgain"/>) when every edit
/// it overlapped is written, else once the last one ends.
/// </summary>
public bool IsCurrent(int token, out bool readAgain)
{
readAgain = false;
if (token == _begun) return true;
if (_writing > 0) _readAfterWrites = true;
else readAgain = true;
return false;
}
}