fix(index): lock the sequence row while an index commit writes it back
ArchiveIndexer.SyncAsync and the ingest's in-step append (and the first frame of an API sequence) read the sequence row and upsert the whole row from that read in one transaction. On PostgreSQL (READ COMMITTED) a PATCH accepted between the read and the upsert was written back over: its settings were lost and settings_version went back, so the PC that pushed them later pulled the old settings over its edit. The row is now read with SequenceRepository.GetForUpdateAsync (FOR UPDATE, as EditAsync takes it). A PATCH committed first is then moved by the insertion like any position; one arriving later sees the new frames version and gets 409 frames_conflict. 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
+46
-3
No files matched your search
@@ -69,6 +69,38 @@ public abstract class SequenceEditTests(TestContext context) : RepositoryTest(co
|
||||
Assert.Equal(SequenceEditOutcome.Conflict, result.Outcome);
|
||||
Assert.Null((await db.Sequences.GetAsync(sequence.Id, Ct))!.HeroFrameId);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnEditWaitsForARowWrittenBackFromALockedRead()
|
||||
{
|
||||
var db = await OpenTitanoAsync();
|
||||
var archive = TestData.Archive();
|
||||
await db.Archives.UpsertAsync(archive, Ct);
|
||||
var sequence = TestData.Sequence(archive.Id);
|
||||
await db.Sequences.UpsertAsync(sequence, Ct);
|
||||
|
||||
// An index commit reads the row and writes it back with its frames; a settings edit (the server's PATCH) arrives meanwhile.
|
||||
Task<SequenceEditResult> edit;
|
||||
await using (var tx = await db.BeginAsync(Ct))
|
||||
{
|
||||
var read = Assert.NotNull(await tx.Sequences.GetForUpdateAsync(sequence.Id, Ct));
|
||||
edit = db.Sequences.EditAsync(sequence.Id, new SequenceEdit { SettingsJson = """{"excludedFrames":[1]}""" }, read.SettingsVersion, read.FramesVersion, Ct);
|
||||
await Task.WhenAny(edit, Task.Delay(300, Ct));
|
||||
Assert.False(edit.IsCompleted, "the edit waits for the row");
|
||||
await tx.Sequences.UpsertAsync(read with { FramesVersion = read.FramesVersion + 1 }, Ct);
|
||||
await tx.CommitAsync(Ct);
|
||||
}
|
||||
|
||||
// It then finds the frames changed: written against the old ones, it is refused rather than undone.
|
||||
var result = await edit;
|
||||
Assert.Equal(SequenceEditOutcome.Conflict, result.Outcome);
|
||||
var stored = (await db.Sequences.GetAsync(sequence.Id, Ct))!;
|
||||
Assert.Equal((sequence.SettingsVersion, sequence.FramesVersion + 1), (stored.SettingsVersion, stored.FramesVersion));
|
||||
|
||||
// Without frames in question, it applies on top of the committed row.
|
||||
var later = await db.Sequences.EditAsync(sequence.Id, new SequenceEdit { Rating = 3 }, stored.SettingsVersion, Ct);
|
||||
Assert.Equal((sequence.SettingsVersion + 1, sequence.FramesVersion + 1, 3), (later.Sequence!.SettingsVersion, later.Sequence.FramesVersion, later.Sequence.Rating));
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class SqliteSequenceEditTests(TestContext context) : SequenceEditTests(context)
|
||||
|
||||
Reference in new issue
Block a user