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

@@ -261,8 +261,16 @@ public sealed class ArchiveMaintenance
ManifestStore.WriteSequence(folder.ManifestPath, SequenceFrames.Update(manifest, changes));
var setIds = bySequence.Where(x => x.Set).Select(x => x.Key).ToList();
var clearIds = bySequence.Where(x => !x.Set).Select(x => x.Key).ToList();
if (setIds.Count > 0) await _db.Frames.UpdateFlagsAsync(setIds, FrameFlags.Duplicate, FrameFlags.None, cancellationToken).ConfigureAwait(false);
if (clearIds.Count > 0) await _db.Frames.UpdateFlagsAsync(clearIds, FrameFlags.None, FrameFlags.Duplicate, cancellationToken).ConfigureAwait(false);
await using (var tx = await _db.BeginAsync(cancellationToken).ConfigureAwait(false))
{
// The row before its frames (SequenceRepository.GetForUpdateAsync), so a scan writing them waits or is waited for.
if (await tx.Sequences.GetForUpdateAsync(sequence.Id, cancellationToken).ConfigureAwait(false) is not null)
{
if (setIds.Count > 0) await tx.Frames.UpdateFlagsAsync(setIds, FrameFlags.Duplicate, FrameFlags.None, cancellationToken).ConfigureAwait(false);
if (clearIds.Count > 0) await tx.Frames.UpdateFlagsAsync(clearIds, FrameFlags.None, FrameFlags.Duplicate, cancellationToken).ConfigureAwait(false);
}
await tx.CommitAsync(cancellationToken).ConfigureAwait(false);
}
if (inStep) await _indexer.RecordStampsAsync(archive, folder, sequence.Id, cancellationToken).ConfigureAwait(false);
}
}
+4 -2
View File
@@ -114,10 +114,12 @@ internal sealed class FrameTiers(TitanoDatabase db, SequenceWriter writer, Archi
ManifestStore.WriteSequence(folder.ManifestPath, manifest);
await using (var tx = await db.BeginAsync(cancellationToken).ConfigureAwait(false))
{
await tx.Frames.UpdateTiersAsync(changed.ConvertAll(f => new FrameTierFields(f.Id, f.Flags, f.PreviewW ?? 0, f.PreviewH ?? 0)), cancellationToken)
.ConfigureAwait(false);
// The row before its frames (SequenceRepository.GetForUpdateAsync): a scan holding the row would otherwise wait
// for these frames while this waits for the row, and PostgreSQL would abort one of them.
await tx.Sequences.RecordTiersAsync(sequence.Id, gained ? SequenceStatus.HasWarnings : SequenceStatus.None,
frames.Count(f => (f.Flags & FrameFlags.PreviewOnly) != 0), cancellationToken).ConfigureAwait(false);
await tx.Frames.UpdateTiersAsync(changed.ConvertAll(f => new FrameTierFields(f.Id, f.Flags, f.PreviewW ?? 0, f.PreviewH ?? 0)), cancellationToken)
.ConfigureAwait(false);
await tx.CommitAsync(cancellationToken).ConfigureAwait(false);
}
if (inStep) await indexer.RecordStampsAsync(archive, folder, sequence.Id, cancellationToken).ConfigureAwait(false);
@@ -171,6 +171,8 @@ public sealed class SequenceRepository
/// <see cref="GetAsync"/> inside a transaction, the row locked until it ends (PostgreSQL; SQLite's write lock already
/// serialises writers): a transaction that writes the whole row back from this read cannot undo an edit that another
/// one commits meanwhile, as <see cref="EditAsync(Guid, SequenceEdit, int?, int?, CancellationToken)"/> takes the same lock.
/// A transaction that writes frames of the sequence takes this lock, or writes the row, before the frames: with one
/// order no two of them wait for each other's rows (PostgreSQL would abort one with a deadlock).
/// </summary>
public Task<Sequence?> GetForUpdateAsync(Guid id, CancellationToken cancellationToken = default) =>
_db.SingleAsync($"{_select} WHERE s.id = $1{_db.Sql.ForUpdate}", [id], Map, cancellationToken);