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:
Claude committed 2026-10-12 17:09:11 +00:00
1 parent b0c364539d
commit 20c08a9250
4 files changed
+46 -3

No files matched your search

+2 -1
View File
@@ -100,7 +100,8 @@ public sealed class ArchiveIndexer
PositionMove? positions = null;
await using (var tx = await _db.BeginAsync(cancellationToken).ConfigureAwait(false))
{
var existing = await tx.Sequences.GetAsync(manifest.Id, cancellationToken).ConfigureAwait(false);
// Locked: a settings edit committed after this read (the server's PATCH takes no lease) would be written back over.
var existing = await tx.Sequences.GetForUpdateAsync(manifest.Id, cancellationToken).ConfigureAwait(false);
if (existing is not null)
{
positions = await ProjectPositions.MoveAsync(tx, existing, manifest, cancellationToken).ConfigureAwait(false);
+4 -2
View File
@@ -195,7 +195,8 @@ public sealed class ArchiveIngest : IArchiveIngest
{
if (row.SettingsJson is { } settings) AtomicFile.WriteAllText(sequence.ProjectPath, settings);
await using var tx = await _db.BeginAsync(cancellationToken).ConfigureAwait(false);
var current = await tx.Sequences.GetAsync(sequenceId, cancellationToken).ConfigureAwait(false)
// Locked, so an edit of the new sequence committed meanwhile is not written back over.
var current = await tx.Sequences.GetForUpdateAsync(sequenceId, cancellationToken).ConfigureAwait(false)
?? throw new InvalidOperationException($"Sequence {sequenceId} was deleted before its first frame.");
await tx.Sequences.UpsertAsync(SequenceMapping.ToSequence(manifest, archive.Id, sequence, current), cancellationToken).ConfigureAwait(false);
await tx.CommitAsync(cancellationToken).ConfigureAwait(false);
@@ -473,7 +474,8 @@ public sealed class ArchiveIngest : IArchiveIngest
PositionMove? placed = null;
await using (var tx = await _db.BeginAsync(cancellationToken).ConfigureAwait(false))
{
var existing = await tx.Sequences.GetAsync(manifest.Id, cancellationToken).ConfigureAwait(false);
// Locked: a settings edit committed after this read (the server's PATCH takes no lease) would be written back over.
var existing = await tx.Sequences.GetForUpdateAsync(manifest.Id, cancellationToken).ConfigureAwait(false);
if (existing is not null && ProjectPositions.Place(existing, manifest) is { } positions)
{
placed = positions;