fix(settings): a read of the archive's settings that overlapped an edit waits for it and reads again
A read of the open archive's settings that an Archive row edit overlapped was dropped and left to the edit's end, so a folder change could complete while the rows still showed the previous archive. The read now waits until no edit is being written and reads again, so the retarget and the reload on showing Settings are done only once the rows show what the archive holds. A smoke test edits a row while Settings reads the archive, with the settings lease held elsewhere. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
4 files changed
+135
-71
No files matched your search
@@ -319,6 +319,9 @@ public sealed class SettingsViewModel : SectionViewModel
|
|||||||
/// <summary>The latest archive folder change: done once the rows show what the folder holds (tests wait for it).</summary>
|
/// <summary>The latest archive folder change: done once the rows show what the folder holds (tests wait for it).</summary>
|
||||||
internal Task ArchiveRetarget => _retarget;
|
internal Task ArchiveRetarget => _retarget;
|
||||||
|
|
||||||
|
/// <summary>The read of the open archive's settings when Settings was last shown: done once the rows show them (tests wait for it).</summary>
|
||||||
|
internal Task ArchiveReload { get; private set; } = Task.CompletedTask;
|
||||||
|
|
||||||
/// <summary>A portable copy leaves nothing in the registry (BRIEF §6.2): it offers no Startup row.</summary>
|
/// <summary>A portable copy leaves nothing in the registry (BRIEF §6.2): it offers no Startup row.</summary>
|
||||||
public bool ShowsStartup => !Shell.Services.Environment.IsPortable;
|
public bool ShowsStartup => !Shell.Services.Environment.IsPortable;
|
||||||
|
|
||||||
@@ -901,7 +904,7 @@ public sealed class SettingsViewModel : SectionViewModel
|
|||||||
if (!DefaultServer.IsDirty) DefaultServer.Load(ServerAddress.Display(_s.ServerUrl));
|
if (!DefaultServer.IsDirty) DefaultServer.Load(ServerAddress.Display(_s.ServerUrl));
|
||||||
OnPropertyChanged(nameof(StartWithWindows));
|
OnPropertyChanged(nameof(StartWithWindows));
|
||||||
UpdateArchiveFree();
|
UpdateArchiveFree();
|
||||||
_ = LoadArchiveAsync();
|
ArchiveReload = LoadArchiveAsync();
|
||||||
if (_page == SettingsPage.Advanced) LoadBackups();
|
if (_page == SettingsPage.Advanced) LoadBackups();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -940,9 +943,9 @@ public sealed class SettingsViewModel : SectionViewModel
|
|||||||
new ProxyPolicy { LongEdge = archive.ProxyLongEdge }, new ThumbnailPolicy { LongEdge = _s.ThumbnailLongEdge });
|
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
|
// 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).
|
// again once no edit is being written (ArchiveReadGate), so this is done only when they show the archive.
|
||||||
if (_archiveReads.IsCurrent(read, out var readAgain)) break;
|
if (_archiveReads.IsCurrent(read)) break;
|
||||||
if (!readAgain) return archive;
|
await _archiveReads.WrittenAsync();
|
||||||
}
|
}
|
||||||
if (!FolderTemplate.IsDirty) FolderTemplate.Load(settings.Templates.Folder);
|
if (!FolderTemplate.IsDirty) FolderTemplate.Load(settings.Templates.Folder);
|
||||||
if (!FileTemplate.IsDirty) FileTemplate.Load(settings.Templates.File);
|
if (!FileTemplate.IsDirty) FileTemplate.Load(settings.Templates.File);
|
||||||
@@ -1016,9 +1019,10 @@ public sealed class SettingsViewModel : SectionViewModel
|
|||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
// A read of the archive's settings this write overlapped was dropped: the rows are read again.
|
// A read of the archive's settings this write overlapped was not shown: it reads again once no edit is written.
|
||||||
if (_archiveReads.EndWrite()) reload = true;
|
_archiveReads.EndWrite();
|
||||||
}
|
}
|
||||||
|
// Only once the write ended: a read begun while it is written waits for that end.
|
||||||
if (reload) await LoadArchiveAsync();
|
if (reload) await LoadArchiveAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ namespace Titano.App.Settings;
|
|||||||
/// Orders Settings › Archive's reads of the open archive's settings (its archive.json and index row) against the edits
|
/// 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
|
/// 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
|
/// 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
|
/// while the archive keeps it. Such a read is not shown (<see cref="IsCurrent"/>): its reader waits until no edit is being
|
||||||
/// once when none is left (<see cref="IsCurrent"/>), else when the last one ends (<see cref="EndWrite"/>). Used from the
|
/// written (<see cref="WrittenAsync"/>) and reads again, so a read is done only once the rows show what the archive holds.
|
||||||
/// interface thread only.
|
/// Used from the interface thread only.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class ArchiveReadGate
|
public sealed class ArchiveReadGate
|
||||||
{
|
{
|
||||||
@@ -15,7 +15,7 @@ public sealed class ArchiveReadGate
|
|||||||
|
|
||||||
private int _begun;
|
private int _begun;
|
||||||
private int _writing;
|
private int _writing;
|
||||||
private bool _readAfterWrites;
|
private TaskCompletionSource? _written;
|
||||||
|
|
||||||
/// <summary>An edit starts being written into the archive.</summary>
|
/// <summary>An edit starts being written into the archive.</summary>
|
||||||
public void BeginWrite()
|
public void BeginWrite()
|
||||||
@@ -24,17 +24,14 @@ public sealed class ArchiveReadGate
|
|||||||
_writing++;
|
_writing++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>The edit was written, or failed. When it was the last one being written, the reads waiting for it go on.</summary>
|
||||||
/// The edit was written, or failed. True when the rows must be read again now: a read it overlapped was dropped and
|
public void EndWrite()
|
||||||
/// no other edit is being written.
|
|
||||||
/// </summary>
|
|
||||||
public bool EndWrite()
|
|
||||||
{
|
{
|
||||||
if (_writing == 0) throw new InvalidOperationException("No edit is being written.");
|
if (_writing == 0) throw new InvalidOperationException("No edit is being written.");
|
||||||
_writing--;
|
if (--_writing > 0) return;
|
||||||
if (_writing > 0 || !_readAfterWrites) return false;
|
var written = _written;
|
||||||
_readAfterWrites = false;
|
_written = null;
|
||||||
return true;
|
written?.SetResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>A read starts; its token tells <see cref="IsCurrent"/> which edits it may have missed.</summary>
|
/// <summary>A read starts; its token tells <see cref="IsCurrent"/> which edits it may have missed.</summary>
|
||||||
@@ -42,15 +39,14 @@ public sealed class ArchiveReadGate
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Whether what a read begun with <paramref name="token"/> found may be shown: no edit was being written when it began
|
/// 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
|
/// and none began since. When it may not, its reader awaits <see cref="WrittenAsync"/> and reads again.
|
||||||
/// it overlapped is written, else once the last one ends.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsCurrent(int token, out bool readAgain)
|
public bool IsCurrent(int token) => token == _begun;
|
||||||
{
|
|
||||||
readAgain = false;
|
/// <summary>
|
||||||
if (token == _begun) return true;
|
/// Done once no edit is being written: at once when none is, else when the last one ends. Its continuations run after
|
||||||
if (_writing > 0) _readAfterWrites = true;
|
/// <see cref="EndWrite"/> returns, never inside it.
|
||||||
else readAgain = true;
|
/// </summary>
|
||||||
return false;
|
public Task WrittenAsync() =>
|
||||||
}
|
_writing == 0 ? Task.CompletedTask : (_written ??= new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously)).Task;
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,8 @@ namespace Titano.App.Tests.Settings;
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Settings › Archive never shows a read of the archive's settings that overlapped an edit being written: on a slow share
|
/// 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
|
/// 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.
|
/// defaults while the archive keeps it. Its reader waits for the edits instead and reads again, so the read is done only
|
||||||
|
/// once the rows show what the archive holds.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class ArchiveReadGateTests
|
public sealed class ArchiveReadGateTests
|
||||||
{
|
{
|
||||||
@@ -14,69 +15,79 @@ public sealed class ArchiveReadGateTests
|
|||||||
public void AReadWithNoEditMeanwhileIsShown()
|
public void AReadWithNoEditMeanwhileIsShown()
|
||||||
{
|
{
|
||||||
var gate = new ArchiveReadGate();
|
var gate = new ArchiveReadGate();
|
||||||
Assert.True(gate.IsCurrent(gate.BeginRead(), out var again));
|
Assert.True(gate.IsCurrent(gate.BeginRead()));
|
||||||
Assert.False(again);
|
Assert.True(gate.WrittenAsync().IsCompleted, "no edit is being written");
|
||||||
|
|
||||||
// An edit written and ended before the read began is in the file it reads.
|
// An edit written and ended before the read began is in the file it reads.
|
||||||
gate.BeginWrite();
|
gate.BeginWrite();
|
||||||
Assert.False(gate.EndWrite(), "no read overlapped it");
|
gate.EndWrite();
|
||||||
Assert.True(gate.IsCurrent(gate.BeginRead(), out again));
|
Assert.True(gate.IsCurrent(gate.BeginRead()));
|
||||||
Assert.False(again);
|
Assert.True(gate.WrittenAsync().IsCompleted);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void AReadThatAnEditOverlapsIsReadAgainWhenTheEditIsWritten()
|
public void AReadThatAnEditOverlapsWaitsForTheEditAndReadsAgain()
|
||||||
{
|
{
|
||||||
// Settings is shown and reads archive.json; the user commits a template before the read answers.
|
// Settings is shown and reads archive.json; the user commits a template before the read answers.
|
||||||
var gate = new ArchiveReadGate();
|
var gate = new ArchiveReadGate();
|
||||||
var read = gate.BeginRead();
|
var read = gate.BeginRead();
|
||||||
gate.BeginWrite();
|
gate.BeginWrite();
|
||||||
Assert.False(gate.IsCurrent(read, out var again), "the read may hold the file before the edit");
|
Assert.False(gate.IsCurrent(read), "the read may hold the file before the edit");
|
||||||
Assert.False(again, "the edit is still being written: its end reads again");
|
var written = gate.WrittenAsync();
|
||||||
Assert.True(gate.EndWrite(), "the edit's end reads the rows again");
|
Assert.False(written.IsCompleted, "the edit is still being written");
|
||||||
|
gate.EndWrite();
|
||||||
|
Assert.True(written.IsCompleted, "the reader reads again once the edit is written");
|
||||||
|
Assert.True(gate.IsCurrent(gate.BeginRead()));
|
||||||
|
|
||||||
// That read is shown, and asks nothing more of later edits.
|
// The edit began and ended while the read was out: the reader reads again at once.
|
||||||
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();
|
read = gate.BeginRead();
|
||||||
Assert.False(gate.EndWrite());
|
gate.BeginWrite();
|
||||||
Assert.False(gate.IsCurrent(read, out again));
|
gate.EndWrite();
|
||||||
Assert.True(again);
|
Assert.False(gate.IsCurrent(read));
|
||||||
Assert.True(gate.IsCurrent(gate.BeginRead(), out again));
|
Assert.True(gate.WrittenAsync().IsCompleted);
|
||||||
|
Assert.True(gate.IsCurrent(gate.BeginRead()));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void OnlyTheLastOfSeveralEditsReadsAgain()
|
public void AReadBegunWhileAnEditIsWrittenIsNeverShown()
|
||||||
|
{
|
||||||
|
// The rows are read again (a folder change, the screen shown) while an edit is still being written.
|
||||||
|
var gate = new ArchiveReadGate();
|
||||||
|
gate.BeginWrite();
|
||||||
|
var read = gate.BeginRead();
|
||||||
|
Assert.False(gate.IsCurrent(read));
|
||||||
|
var written = gate.WrittenAsync();
|
||||||
|
gate.EndWrite();
|
||||||
|
Assert.False(gate.IsCurrent(read), "it began before the edit was written");
|
||||||
|
Assert.True(written.IsCompleted);
|
||||||
|
Assert.True(gate.IsCurrent(gate.BeginRead()));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task AReaderGoesOnOnlyOnceTheLastOfSeveralEditsIsWrittenAndNeverInsideIt()
|
||||||
{
|
{
|
||||||
// A proxy size and a thumbnail size picked one after the other, both overlapping a read.
|
// A proxy size and a thumbnail size picked one after the other, both overlapping a read.
|
||||||
var gate = new ArchiveReadGate();
|
var gate = new ArchiveReadGate();
|
||||||
var read = gate.BeginRead();
|
var read = gate.BeginRead();
|
||||||
gate.BeginWrite();
|
gate.BeginWrite();
|
||||||
gate.BeginWrite();
|
gate.BeginWrite();
|
||||||
Assert.False(gate.IsCurrent(read, out var again));
|
Assert.False(gate.IsCurrent(read));
|
||||||
Assert.False(again);
|
var written = gate.WrittenAsync();
|
||||||
Assert.False(gate.EndWrite(), "another edit is still being written");
|
Assert.Same(written, gate.WrittenAsync());
|
||||||
// A read begun meanwhile is dropped too, and left to the last edit.
|
gate.EndWrite();
|
||||||
read = gate.BeginRead();
|
Assert.False(written.IsCompleted, "another edit is still being written");
|
||||||
Assert.False(gate.IsCurrent(read, out again));
|
|
||||||
Assert.False(again);
|
// The reader goes on once the last edit ends, never inside its EndWrite: the writer finishes first.
|
||||||
Assert.True(gate.EndWrite());
|
var thread = Environment.CurrentManagedThreadId;
|
||||||
|
var ending = false;
|
||||||
|
var inside = written.ContinueWith(_ => ending && Environment.CurrentManagedThreadId == thread, CancellationToken.None,
|
||||||
|
TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
|
||||||
|
ending = true;
|
||||||
|
gate.EndWrite();
|
||||||
|
ending = false;
|
||||||
|
Assert.True(written.IsCompleted);
|
||||||
|
Assert.False(await inside, "the reader did not go on inside EndWrite");
|
||||||
|
Assert.True(gate.IsCurrent(gate.BeginRead()));
|
||||||
Assert.Throws<InvalidOperationException>(() => gate.EndWrite());
|
Assert.Throws<InvalidOperationException>(() => gate.EndWrite());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -217,9 +217,12 @@ public sealed class SettingsScreenSmokeTests
|
|||||||
settings.ArchiveRoot.Text = otherRoot;
|
settings.ArchiveRoot.Text = otherRoot;
|
||||||
Assert.True(settings.ArchiveRoot.Commit());
|
Assert.True(settings.ArchiveRoot.Commit());
|
||||||
await WaitAsync(() => Read(manifestPath).LayoutTemplates.File == "{date}-{n:0000}", "the edit reached the archive of the folder it was made for");
|
await WaitAsync(() => Read(manifestPath).LayoutTemplates.File == "{date}-{n:0000}", "the edit reached the archive of the folder it was made for");
|
||||||
|
// The folder change is done only once the rows show that folder's archive, also when its read overlapped the
|
||||||
|
// edit written into the first archive: that read waits for the edit and reads again instead of leaving the rows.
|
||||||
await settings.ArchiveRetarget;
|
await settings.ArchiveRetarget;
|
||||||
Assert.NotEqual("{date}-{n:0000}", Read(otherManifestPath).LayoutTemplates.File);
|
Assert.NotEqual("{date}-{n:0000}", Read(otherManifestPath).LayoutTemplates.File);
|
||||||
Assert.Equal(Read(otherManifestPath).LayoutTemplates.File, settings.FileTemplate.Text);
|
Assert.Equal(Read(otherManifestPath).LayoutTemplates.File, settings.FileTemplate.Text);
|
||||||
|
Assert.Equal(Read(otherManifestPath).LayoutTemplates.File, shell.Services.Settings.FileTemplate);
|
||||||
settings.ArchiveRoot.Text = archive.RootPath;
|
settings.ArchiveRoot.Text = archive.RootPath;
|
||||||
Assert.True(settings.ArchiveRoot.Commit());
|
Assert.True(settings.ArchiveRoot.Commit());
|
||||||
await settings.ArchiveRetarget;
|
await settings.ArchiveRetarget;
|
||||||
@@ -239,6 +242,56 @@ public sealed class SettingsScreenSmokeTests
|
|||||||
Assert.Equal(480, settings.SelectedThumbnailSize.Value);
|
Assert.Equal(480, settings.SelectedThumbnailSize.Value);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public Task AnArchiveRowEditedWhileSettingsReadsTheArchiveIsNotUndoneByThatRead() => ScreenHost.RunAsync(async () =>
|
||||||
|
{
|
||||||
|
await using var app = await ScreenSmokeTests.SampleApp.StartAsync();
|
||||||
|
var shell = app.Shell;
|
||||||
|
await app.Sample.FillAsync();
|
||||||
|
var settings = shell.Settings;
|
||||||
|
var archive = Assert.NotNull(await Titano.App.Archiving.CurrentArchive.FindAsync(shell.Services.Database!, null));
|
||||||
|
var paths = new Titano.Core.Archiving.ArchivePaths(archive.RootPath);
|
||||||
|
static Titano.Core.Archiving.ArchiveManifest Read(string path) => Titano.Core.Archiving.ManifestStore.ReadArchive(path);
|
||||||
|
shell.Navigate(SectionKey.Settings);
|
||||||
|
settings.Page = SettingsPage.Archive;
|
||||||
|
settings.ArchiveRoot.Text = archive.RootPath;
|
||||||
|
Assert.True(settings.ArchiveRoot.Commit());
|
||||||
|
await settings.ArchiveRetarget;
|
||||||
|
var before = Read(paths.ManifestPath).LayoutTemplates.File;
|
||||||
|
const string edited = "{camera}_{n:00000}";
|
||||||
|
Assert.NotEqual(edited, before);
|
||||||
|
Assert.Equal(before, settings.FileTemplate.Text);
|
||||||
|
|
||||||
|
// Another PC holds the archive's settings, so the edit waits to be written. Settings is shown again just before
|
||||||
|
// the edit is committed: its read of archive.json answers meanwhile, with the file from before the edit.
|
||||||
|
var lease = Assert.NotNull(Titano.Core.Archiving.LeaseFile.TryAcquire(paths.LocksDirectory, "archive-settings", "other-pc"));
|
||||||
|
Task reload;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
shell.Navigate(SectionKey.Library);
|
||||||
|
shell.Navigate(SectionKey.Settings);
|
||||||
|
reload = settings.ArchiveReload;
|
||||||
|
Assert.False(reload.IsCompleted, "the read is in flight");
|
||||||
|
settings.FileTemplate.Text = edited;
|
||||||
|
Assert.True(settings.FileTemplate.Commit());
|
||||||
|
await Task.WhenAny(reload, Task.Delay(1500));
|
||||||
|
Assert.False(reload.IsCompleted, "the read waits for the edit it overlapped");
|
||||||
|
Assert.Equal(edited, settings.FileTemplate.Text);
|
||||||
|
Assert.Equal(edited, shell.Services.Settings.FileTemplate);
|
||||||
|
Assert.Equal(before, Read(paths.ManifestPath).LayoutTemplates.File);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
lease.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Once the edit is written, the read is made again and shows it.
|
||||||
|
await reload.WaitAsync(TimeSpan.FromSeconds(20));
|
||||||
|
Assert.Equal(edited, Read(paths.ManifestPath).LayoutTemplates.File);
|
||||||
|
Assert.Equal(edited, settings.FileTemplate.Text);
|
||||||
|
Assert.Equal(edited, shell.Services.Settings.FileTemplate);
|
||||||
|
});
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public Task TheFirstRunDialogKeepsTheKeyboardInside() => ScreenHost.RunAsync(async () =>
|
public Task TheFirstRunDialogKeepsTheKeyboardInside() => ScreenHost.RunAsync(async () =>
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user