diff --git a/src/Titano.Core/Archiving/ThumbPack.cs b/src/Titano.Core/Archiving/ThumbPack.cs
index 48bf29df..c2bf8eaa 100644
--- a/src/Titano.Core/Archiving/ThumbPack.cs
+++ b/src/Titano.Core/Archiving/ThumbPack.cs
@@ -279,7 +279,7 @@ public sealed class ThumbPackReader : IDisposable
public void Dispose() => _stream?.Dispose();
}
-/// Appends thumbnails to a pack; one writer per pack at a time (the sequence lease guarantees it).
+/// Appends thumbnails to a pack; one writer per pack at a time (the thumbs-<sequence id> lease guarantees it).
public sealed class ThumbPackWriter : IDisposable
{
private readonly FileStream _stream;
diff --git a/src/Titano.Data/Archiving/ArchiveMaintenance.cs b/src/Titano.Data/Archiving/ArchiveMaintenance.cs
index 7e598f2d..26578022 100644
--- a/src/Titano.Data/Archiving/ArchiveMaintenance.cs
+++ b/src/Titano.Data/Archiving/ArchiveMaintenance.cs
@@ -188,7 +188,11 @@ public sealed class ArchiveMaintenance
if (length == 0 || garbage < length * CompactionRatio) continue;
compacted.Add(location.Folder);
if (dryRun) continue;
+ // The thumbnail builder appends under the thumbs lease only: holding it too keeps the compacted pack from
+ // replacing the file under a writer, whose later thumbnails would land in the orphaned one.
await using (await _writer.LockAsync(paths, location.Id, cancellationToken).ConfigureAwait(false))
+ using (await LeaseFile.AcquireAsync(paths.LocksDirectory, $"thumbs-{location.Id:N}", _writer.Options.Owner, _writer.Options.LeaseWait,
+ _writer.Options.LeaseDuration, cancellationToken).ConfigureAwait(false))
ThumbPackWriter.Compact(sequence.ThumbsPackPath);
}
return compacted;
diff --git a/tests/Titano.Data.Tests/Archiving/ArchiveIntegrityTests.cs b/tests/Titano.Data.Tests/Archiving/ArchiveIntegrityTests.cs
index 87a0215a..e73e5444 100644
--- a/tests/Titano.Data.Tests/Archiving/ArchiveIntegrityTests.cs
+++ b/tests/Titano.Data.Tests/Archiving/ArchiveIntegrityTests.cs
@@ -150,6 +150,38 @@ public sealed class ArchiveIntegrityTests(TestContext context)
Assert.FileMissing(Path.Combine(fx.Paths.LocksDirectory, "seq-dead.lease"));
Assert.True(folder.Frames().All(File.Exists));
}
+
+ [Test]
+ public async Task PackCompactionWaitsForTheThumbnailWriter()
+ {
+ await using var fx = await CreateAsync(context);
+ var id = await IngestSeriesAsync(fx, "a", 2, 1);
+ var folder = fx.Paths.Sequence((await fx.Db.Sequences.GetAsync(id))!.Folder);
+ var hash = new byte[ThumbPack.HashLength];
+ using (var pack = ThumbPackWriter.Open(folder.ThumbsPackPath))
+ {
+ // The same entry written again and again: everything but the last copy is garbage.
+ for (var i = 0; i < 4; i++) pack.Append(0, 48, 32, hash, Photos.Jpeg(Start, 70 + i));
+ pack.Commit();
+ }
+ var length = new FileInfo(folder.ThumbsPackPath).Length;
+
+ // A thumbnail pass holds the pack's own lease (not the sequence's) while it appends.
+ Task maintenance;
+ using (await LeaseFile.AcquireAsync(fx.Paths.LocksDirectory, $"thumbs-{id:N}", "thumbnails", TimeSpan.FromSeconds(5), cancellationToken: fx.Ct))
+ {
+ maintenance = fx.Service.Maintenance.RunAsync(fx.Archive, dryRun: false, 0, _ => ValueTask.CompletedTask, fx.Ct);
+ await Task.Delay(500, fx.Ct);
+ Assert.False(maintenance.IsCompleted);
+ Assert.Equal(length, new FileInfo(folder.ThumbsPackPath).Length);
+ }
+
+ var report = await maintenance;
+ Assert.SequenceEqual([folder.RelativeFolder], report.CompactedPacks);
+ using var compacted = ThumbPackReader.Open(folder.ThumbsPackPath);
+ Assert.Equal(0L, compacted.GarbageBytes);
+ Assert.NotNull(compacted.Read(0));
+ }
}
internal static class SequencePathsExtensions