fix(archive): lock the sequence row before its frames in every index write

FrameTiers.RecordAsync updated the frames of a sequence before its row,
while a scan's sync locks the row (FOR UPDATE) before rewriting every
frame. On PostgreSQL the two deadlocked and one job was aborted with
40P01. The tier writer now records the row first; the verify's integrity
flags (next commit) and maintenance's duplicate flags take the row lock
in the same transaction as their frames, so every writer of a sequence's
frames follows one order. A PostgreSQL test that deadlocked with the old
order covers it.

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 21:34:08 +00:00
1 parent bb2f40c29f
commit e369d1d20a
4 files changed
+45 -6

No files matched your search

@@ -11,8 +11,9 @@ namespace Titano.Data.Tests.Archiving;
/// <summary>
/// Writers that change part of a sequence's sidecar and index row (decode tiers, the verify, title, tags, rating and
/// notes, a rename) while a settings edit or another writer commits, on both dialects: they write only what they own,
/// under the sequence lease, and leave what another process changed in the sidecar to the next scan.
/// notes, a rename, an upload) while a settings edit, a scan or another writer commits, on both dialects: they write only
/// what they own, under the sequence lease, take the row before the frames, and leave what another process changed in
/// the sidecar to the next scan or index it whole.
/// </summary>
[Category("archive")]
public abstract class PartialRowWriterTests(TestContext context) : DatabaseTest(context)
@@ -76,6 +77,32 @@ public abstract class PartialRowWriterTests(TestContext context) : DatabaseTest(
Assert.Equal(FrameFlags.PreviewOnly, (await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct))[0].Flags);
}
[Test]
public async Task TiersRecordedWhileAScanHoldsTheRowWaitForItBeforeTheFrames()
{
var fx = await FixtureAsync();
var sequence = await IndexedAsync(fx);
var frames = await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct);
// A scan's sync holds the row, as it does before it rewrites the frames, while an analysis records a tier.
Task record;
await using (var tx = await fx.Db.BeginAsync(fx.Ct))
{
await tx.Sequences.GetForUpdateAsync(sequence.Id, fx.Ct);
record = Tiers(fx).RecordAsync(fx.Archive, sequence, Preview(frames[0].Id), proxies: false, fx.Ct);
await Task.WhenAny(record, Task.Delay(500, fx.Ct));
Assert.False(record.IsCompleted, "the record waits for the row");
// The record touched no frame yet, so the scan rewrites them all without waiting for it. Had it locked its frame
// before the row, each would wait for the other and PostgreSQL would abort one of them.
await tx.Frames.SyncSequenceAsync(sequence.Id, frames, fx.Ct);
await tx.CommitAsync(fx.Ct);
}
await record;
Assert.Equal(FrameFlags.PreviewOnly, (await fx.Db.Frames.GetAsync(frames[0].Id, fx.Ct))!.Flags);
Assert.Equal(1, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.PreviewOnlyCount);
}
[Test]
public async Task TiersRecordedAfterAnotherWriterAddedFramesLeaveThoseFramesToTheNextScan()
{