fix(data): write a sequence's tags in one order on PostgreSQL

Each tag upsert locks its row until commit, and tags were written in the
order the caller listed them. A PATCH listing them in another order than
an upload, a scan or another edit of a sequence sharing two of them could
lock the rows in opposite orders, and PostgreSQL aborted one writer with a
deadlock that nothing retries. Tags are now written in the order of their
keys, and a set the sequence already carries is not written again.

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 23:28:40 +00:00
1 parent e6129d2a11
commit 2cdc44abe4
2 files changed
+58 -2

No files matched your search

@@ -500,11 +500,22 @@ public sealed class SequenceRepository
await session.ScalarAsync<Guid?>($"SELECT id FROM sequences WHERE id = $1{SqlDialect.For(session.Dialect).ForUpdate}", [id], cancellationToken)
.ConfigureAwait(false) is not null;
/// <summary>Replaces the tag links of a sequence whose row the caller has locked (or just upserted).</summary>
/// <summary>
/// Replaces the tag links of a sequence whose row the caller has locked (or just upserted); a set the sequence already
/// carries (names compared case-insensitively) is left as it is. On PostgreSQL every tag written is locked until
/// commit, and the tags are written in the order of their keys whatever order the caller listed them in (a PATCH sends
/// them in any order): writers of two sequences that share tags then take those rows in one order, and never wait for
/// each other's (PostgreSQL would abort one with a deadlock, and nothing retries it).
/// </summary>
private async Task ReplaceTagsAsync(IDbSession session, Guid sequenceId, IEnumerable<string> tags, CancellationToken cancellationToken)
{
var wanted = TagRepository.NormalizeSet(tags);
var keys = wanted.Select(TagRepository.Key).ToList();
var stored = await session.QueryListAsync("SELECT t.name_key FROM sequence_tags st JOIN tags t ON t.id = st.tag_id WHERE st.sequence_id = $1",
[sequenceId], r => r.GetString(0), cancellationToken).ConfigureAwait(false);
if (stored.Count == keys.Count && stored.ToHashSet(StringComparer.Ordinal).SetEquals(keys)) return;
await session.ExecuteAsync("DELETE FROM sequence_tags WHERE sequence_id = $1", [sequenceId], cancellationToken).ConfigureAwait(false);
foreach (var tag in TagRepository.NormalizeSet(tags))
foreach (var tag in wanted.OrderBy(TagRepository.Key, StringComparer.Ordinal))
{
DbParam[] args = [Guid.CreateVersion7(), tag, TagRepository.Key(tag)];
Guid tagId;
@@ -101,6 +101,51 @@ public abstract class SequenceEditTests(TestContext context) : RepositoryTest(co
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));
}
[Test]
public async Task WritersOfSequencesSharingTagsListedInOppositeOrdersBothComplete()
{
var db = await OpenTitanoAsync(multiConnection: true);
var archive = TestData.Archive();
await db.Archives.UpsertAsync(archive, Ct);
var tagged = TestData.Sequence(archive.Id, title: "Tagged");
var first = TestData.Sequence(archive.Id, title: "First");
var second = TestData.Sequence(archive.Id, title: "Second");
await db.Sequences.UpsertAsync(tagged with { Tags = ["Alps", "sunset"] }, Ct);
await db.Sequences.UpsertAsync(first, Ct);
await db.Sequences.UpsertAsync(second, Ct);
var alps = (await db.Tags.ListAsync(Ct)).Single(t => t.Name == "Alps");
// A transaction holds the first of the two tags (SQLite: its write lock), so both writers below wait until it ends.
Task<SequenceEditResult> forward, backward;
await using (var tx = await db.BeginAsync(Ct))
{
if (Dialect == Dialect.Postgres)
Assert.Equal(alps.Id, await tx.Session.ScalarAsync<Guid?>("SELECT id FROM tags WHERE id = $1 FOR UPDATE", [alps.Id], Ct));
else
Assert.True(await tx.Tags.RenameAsync(alps.Id, "Alps", Ct));
forward = db.Sequences.EditAsync(first.Id, new SequenceEdit { Tags = ["Alps", "sunset"] }, null, Ct);
await Task.WhenAny(forward, Task.Delay(300, Ct));
// The other writer lists the tags the other way round (a PATCH sends them in any order).
backward = db.Sequences.EditAsync(second.Id, new SequenceEdit { Tags = ["SUNSET", "alps"] }, null, Ct);
await Task.WhenAny(backward, Task.Delay(300, Ct));
Assert.False(forward.IsCompleted || backward.IsCompleted, "both writers wait for the tag");
if (Dialect == Dialect.Postgres)
{
// A writer whose tags are already the stored ones takes no tag at all.
var unchanged = db.Sequences.EditAsync(tagged.Id, new SequenceEdit { Tags = ["sunset", "ALPS"], Rating = 2 }, null, Ct);
Assert.Equal(unchanged, await Task.WhenAny(unchanged, Task.Delay(TimeSpan.FromSeconds(20), Ct)));
Assert.Equal(SequenceEditOutcome.Updated, (await unchanged).Outcome);
}
await tx.CommitAsync(Ct);
}
// Both take the tags in one order: neither is aborted for a deadlock.
Assert.Equal((SequenceEditOutcome.Updated, SequenceEditOutcome.Updated), ((await forward).Outcome, (await backward).Outcome));
foreach (var sequence in new[] { tagged, first, second })
Assert.SequenceEqual(["Alps", "sunset"], (await db.Sequences.GetAsync(sequence.Id, Ct))!.Tags);
Assert.Count(2, await db.Tags.ListAsync(Ct));
}
}
public sealed class SqliteSequenceEditTests(TestContext context) : SequenceEditTests(context)