fix: make default initialization concurrency-safe

This commit is contained in:
cesnimda
2026-09-22 07:33:51 +02:00
parent 38cf37ef96
commit 57a6bd02c6
4 changed files with 187 additions and 15 deletions
@@ -357,6 +357,7 @@ public sealed class ApplicationChecklistService : IApplicationChecklistService
var missing = Defaults.Where(t => !existing.Contains(t.Key)).ToList();
if (missing.Count == 0) return items;
var seeded = new List<ApplicationChecklistItem>(missing.Count);
var order = 0;
foreach (var template in Defaults)
{
@@ -377,12 +378,27 @@ public sealed class ApplicationChecklistService : IApplicationChecklistService
};
_db.ApplicationChecklistItems.Add(item);
items.Add(item);
seeded.Add(item);
}
order++;
}
await _db.SaveChangesAsync(ct);
return items;
try
{
await _db.SaveChangesAsync(ct);
return items;
}
catch (DbUpdateException)
{
foreach (var item in seeded) _db.Entry(item).State = EntityState.Detached;
var persisted = await LoadItemsAsync(ownerUserId, jobApplicationId, ct);
var persistedKeys = persisted.Where(i => i.SystemKey is not null)
.Select(i => i.SystemKey!)
.ToHashSet(StringComparer.Ordinal);
if (missing.Any(template => !persistedKeys.Contains(template.Key))) throw;
return persisted;
}
}
private async Task SyncAutoCompletionAsync(List<ApplicationChecklistItem> items, ChecklistSignals signals, CancellationToken ct)