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>
|
||||
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>
|
||||
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));
|
||||
OnPropertyChanged(nameof(StartWithWindows));
|
||||
UpdateArchiveFree();
|
||||
_ = LoadArchiveAsync();
|
||||
ArchiveReload = LoadArchiveAsync();
|
||||
if (_page == SettingsPage.Advanced) LoadBackups();
|
||||
}
|
||||
|
||||
@@ -940,9 +943,9 @@ public sealed class SettingsViewModel : SectionViewModel
|
||||
new ProxyPolicy { LongEdge = archive.ProxyLongEdge }, new ThumbnailPolicy { LongEdge = _s.ThumbnailLongEdge });
|
||||
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;
|
||||
// again once no edit is being written (ArchiveReadGate), so this is done only when they show the archive.
|
||||
if (_archiveReads.IsCurrent(read)) break;
|
||||
await _archiveReads.WrittenAsync();
|
||||
}
|
||||
if (!FolderTemplate.IsDirty) FolderTemplate.Load(settings.Templates.Folder);
|
||||
if (!FileTemplate.IsDirty) FileTemplate.Load(settings.Templates.File);
|
||||
@@ -1016,9 +1019,10 @@ public sealed class SettingsViewModel : SectionViewModel
|
||||
}
|
||||
finally
|
||||
{
|
||||
// A read of the archive's settings this write overlapped was dropped: the rows are read again.
|
||||
if (_archiveReads.EndWrite()) reload = true;
|
||||
// A read of the archive's settings this write overlapped was not shown: it reads again once no edit is written.
|
||||
_archiveReads.EndWrite();
|
||||
}
|
||||
// Only once the write ended: a read begun while it is written waits for that end.
|
||||
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
|
||||
/// 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.
|
||||
/// while the archive keeps it. Such a read is not shown (<see cref="IsCurrent"/>): its reader waits until no edit is being
|
||||
/// written (<see cref="WrittenAsync"/>) and reads again, so a read is done only once the rows show what the archive holds.
|
||||
/// Used from the interface thread only.
|
||||
/// </summary>
|
||||
public sealed class ArchiveReadGate
|
||||
{
|
||||
@@ -15,7 +15,7 @@ public sealed class ArchiveReadGate
|
||||
|
||||
private int _begun;
|
||||
private int _writing;
|
||||
private bool _readAfterWrites;
|
||||
private TaskCompletionSource? _written;
|
||||
|
||||
/// <summary>An edit starts being written into the archive.</summary>
|
||||
public void BeginWrite()
|
||||
@@ -24,17 +24,14 @@ public sealed class ArchiveReadGate
|
||||
_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()
|
||||
/// <summary>The edit was written, or failed. When it was the last one being written, the reads waiting for it go on.</summary>
|
||||
public void EndWrite()
|
||||
{
|
||||
if (_writing == 0) throw new InvalidOperationException("No edit is being written.");
|
||||
_writing--;
|
||||
if (_writing > 0 || !_readAfterWrites) return false;
|
||||
_readAfterWrites = false;
|
||||
return true;
|
||||
if (--_writing > 0) return;
|
||||
var written = _written;
|
||||
_written = null;
|
||||
written?.SetResult();
|
||||
}
|
||||
|
||||
/// <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>
|
||||
/// 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.
|
||||
/// and none began since. When it may not, its reader awaits <see cref="WrittenAsync"/> and reads again.
|
||||
/// </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;
|
||||
}
|
||||
public bool IsCurrent(int token) => token == _begun;
|
||||
|
||||
/// <summary>
|
||||
/// Done once no edit is being written: at once when none is, else when the last one ends. Its continuations run after
|
||||
/// <see cref="EndWrite"/> returns, never inside it.
|
||||
/// </summary>
|
||||
public Task WrittenAsync() =>
|
||||
_writing == 0 ? Task.CompletedTask : (_written ??= new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously)).Task;
|
||||
}
|
||||
Reference in new issue
Block a user