diff --git a/src/Titano.App/Sections/SettingsViewModel.cs b/src/Titano.App/Sections/SettingsViewModel.cs index 72fbc458..36b24d14 100644 --- a/src/Titano.App/Sections/SettingsViewModel.cs +++ b/src/Titano.App/Sections/SettingsViewModel.cs @@ -51,6 +51,9 @@ public sealed class SettingsViewModel : SectionViewModel /// there: edits of the Archive rows made meanwhile wait for it (). /// private Task _retarget = Task.FromResult(null); + + /// Drops a read of the open archive's settings that may have missed an edit being written meanwhile. + private readonly ArchiveReadGate _archiveReads = new(); private bool _isDetectingFormats; private IReadOnlyList _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(); } /// diff --git a/src/Titano.App/Settings/ArchiveReadGate.cs b/src/Titano.App/Settings/ArchiveReadGate.cs new file mode 100644 index 00000000..a4c3cadc --- /dev/null +++ b/src/Titano.App/Settings/ArchiveReadGate.cs @@ -0,0 +1,56 @@ +namespace Titano.App.Settings; + +/// +/// 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 (), else when the last one ends (). Used from the +/// interface thread only. +/// +public sealed class ArchiveReadGate +{ + /// The token of a read begun while an edit was being written: never current. + private const int Overlapped = -1; + + private int _begun; + private int _writing; + private bool _readAfterWrites; + + /// An edit starts being written into the archive. + public void BeginWrite() + { + _begun++; + _writing++; + } + + /// + /// 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. + /// + 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; + } + + /// A read starts; its token tells which edits it may have missed. + public int BeginRead() => _writing > 0 ? Overlapped : _begun; + + /// + /// Whether what a read begun with 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 () when every edit + /// it overlapped is written, else once the last one ends. + /// + 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; + } +} diff --git a/tests/Titano.App.Tests/Settings/ArchiveReadGateTests.cs b/tests/Titano.App.Tests/Settings/ArchiveReadGateTests.cs new file mode 100644 index 00000000..c642eb0d --- /dev/null +++ b/tests/Titano.App.Tests/Settings/ArchiveReadGateTests.cs @@ -0,0 +1,82 @@ +using Titano.App.Settings; +using Titano.Testing; + +namespace Titano.App.Tests.Settings; + +/// +/// Settings › Archive never shows a read of the archive's settings that overlapped an edit being written: on a slow share +/// that read can hold archive.json as it was before the edit, and would undo the edit on the rows and in this PC's +/// defaults while the archive keeps it. +/// +public sealed class ArchiveReadGateTests +{ + [Test] + public void AReadWithNoEditMeanwhileIsShown() + { + var gate = new ArchiveReadGate(); + Assert.True(gate.IsCurrent(gate.BeginRead(), out var again)); + Assert.False(again); + + // An edit written and ended before the read began is in the file it reads. + gate.BeginWrite(); + Assert.False(gate.EndWrite(), "no read overlapped it"); + Assert.True(gate.IsCurrent(gate.BeginRead(), out again)); + Assert.False(again); + } + + [Test] + public void AReadThatAnEditOverlapsIsReadAgainWhenTheEditIsWritten() + { + // Settings is shown and reads archive.json; the user commits a template before the read answers. + var gate = new ArchiveReadGate(); + var read = gate.BeginRead(); + gate.BeginWrite(); + Assert.False(gate.IsCurrent(read, out var again), "the read may hold the file before the edit"); + Assert.False(again, "the edit is still being written: its end reads again"); + Assert.True(gate.EndWrite(), "the edit's end reads the rows again"); + + // That read is shown, and asks nothing more of later edits. + Assert.True(gate.IsCurrent(gate.BeginRead(), out again)); + gate.BeginWrite(); + Assert.False(gate.EndWrite()); + } + + [Test] + public void AReadOverlappingAnEditAlreadyWrittenIsReadAgainAtOnce() + { + // The edit began and ended while the read was out. + var gate = new ArchiveReadGate(); + var read = gate.BeginRead(); + gate.BeginWrite(); + Assert.False(gate.EndWrite(), "the read was not dropped yet"); + Assert.False(gate.IsCurrent(read, out var again)); + Assert.True(again, "nothing else reads again: the read does"); + + // A read that began while an edit was being written, which ended before the read answered. + gate.BeginWrite(); + read = gate.BeginRead(); + Assert.False(gate.EndWrite()); + Assert.False(gate.IsCurrent(read, out again)); + Assert.True(again); + Assert.True(gate.IsCurrent(gate.BeginRead(), out again)); + } + + [Test] + public void OnlyTheLastOfSeveralEditsReadsAgain() + { + // A proxy size and a thumbnail size picked one after the other, both overlapping a read. + var gate = new ArchiveReadGate(); + var read = gate.BeginRead(); + gate.BeginWrite(); + gate.BeginWrite(); + Assert.False(gate.IsCurrent(read, out var again)); + Assert.False(again); + Assert.False(gate.EndWrite(), "another edit is still being written"); + // A read begun meanwhile is dropped too, and left to the last edit. + read = gate.BeginRead(); + Assert.False(gate.IsCurrent(read, out again)); + Assert.False(again); + Assert.True(gate.EndWrite()); + Assert.Throws(() => gate.EndWrite()); + } +}