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:
Claude committed 2026-10-12 19:25:32 +00:00
1 parent c341c264e1
commit dbbb5079d1
4 files changed
+135 -71

No files matched your search

+19 -23
View File
@@ -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;
}