fix(data): light statistics lock their sequences before their frames

FrameRepository.UpdateLightStatsAsync updated every frame of a sequence
in batches without locking the sequence row. On PostgreSQL a scan's or
rebuild's sync holds the row and rewrites every frame in its own order,
so a deep analysis writing its statistics at the same time could hold
some frames while waiting for others the sync held, and PostgreSQL
aborted one of the two jobs with a deadlock.

On PostgreSQL the method now locks the rows of its frames' sequences
first, in the same transaction, like every other writer of a sequence's
frames. SQLite's write lock already serialises writers.

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 22:08:05 +00:00
1 parent bf93434bec
commit 343ef76798
2 files changed
+46 -1

No files matched your search

@@ -115,6 +115,31 @@ public abstract class PartialRowWriterTests(TestContext context) : DatabaseTest(
Assert.Equal(1, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.PreviewOnlyCount);
}
[Test]
public async Task LightStatsWrittenWhileAScanHoldsTheRowWaitForItBeforeTheFrames()
{
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 a light analysis writes its statistics.
Task<int> write;
await using (var tx = await fx.Db.BeginAsync(fx.Ct))
{
await tx.Sequences.GetForUpdateAsync(sequence.Id, fx.Ct);
write = fx.Db.Frames.UpdateLightStatsAsync([.. frames.Select(f => new FrameLightStats(f.Id, -1.5, 0.01, 0.02))], fx.Ct);
await Task.WhenAny(write, Task.Delay(500, fx.Ct));
Assert.False(write.IsCompleted, "the statistics wait for the row");
// The statistics locked no frame yet, so the scan rewrites them all without waiting for them. Frames locked before
// the row, in another order than the scan's, would each wait for the other, and PostgreSQL would abort one.
await tx.Frames.SyncSequenceAsync(sequence.Id, frames, fx.Ct);
await tx.CommitAsync(fx.Ct);
}
Assert.Equal(frames.Count, await write);
Assert.True((await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct)).All(f => f.MeanLogLuminance == -1.5), "written");
}
[Test]
public async Task TiersRecordedAfterAnotherWriterAddedFramesLeaveThoseFramesToTheNextScan()
{