fix(review): close post-commit cancellation window + guard default-deco enqueue (Codex Medium/Low)
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 4s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 3m55s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 6m58s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped

Independent Codex review of the fix diff surfaced two real findings the fork pass missed:

- Medium: the #251 affected-playout QUERIES in ReplaceDecoTemplateItemsHandler and
  UpdateDecoHandler still ran on the request `cancellationToken`, so a cancellation
  landing after SaveChanges committed but before those queries executed would throw
  before the CancellationToken.None enqueue — the edit committed but no playout Reset,
  re-opening the stale-content bug in that window. Run the entire post-commit
  invalidation (queries + enqueue) on CancellationToken.None so the side effect can't
  be half-aborted once the data has changed.
- Low: UpdateDefaultDecoHandler enqueued a Reset for request.PlayoutId even when
  ExecuteUpdateAsync matched 0 rows (nonexistent playout), creating a background build
  request for an id that isn't there. Guard the enqueue on rows-updated > 0 so the
  enqueued set equals the affected set. Added a regression test.

Also corrected the ReplaceProgramScheduleItemsHandler comments: the schedule-item
hierarchy is TPT (table-per-type), not TPH — the SetValues reconcile is safe either way
(same-runtime-type guard; no discriminator to corrupt), Codex confirmed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-11 15:18:38 +02:00
co-authored by Claude Opus 4.8
parent 64473b136d
commit 51ac11e5aa
5 changed files with 36 additions and 17 deletions
@@ -64,7 +64,7 @@ public class ReplaceProgramScheduleItemsHandler(
ProgramScheduleItem built = BuildItem(programSchedule, i, orderedItems[i]);
if (existing.GetType() == built.GetType())
{
// same TPH subtype: copy all scalar values in place (BuildItem is the single source of
// same TPT subtype: copy all scalar values in place (BuildItem is the single source of
// item construction, so no field is silently dropped) and rebuild the join rows, keeping
// the item's id — and with it the fill-group index that would otherwise cascade away.
built.Id = existing.Id;
@@ -73,7 +73,7 @@ public class ReplaceProgramScheduleItemsHandler(
}
else
{
// EF can't change a TPH row's type in place; this slot must be replaced (its fill-group
// EF can't change a TPT row's type in place; this slot must be replaced (its fill-group
// index resets, which is acceptable — the item fundamentally changed).
dbContext.Remove(existing);
programSchedule.Items.Add(built);
@@ -41,14 +41,15 @@ public class ReplaceDecoTemplateItemsHandler(
// Deco/break/default-filler content is only (re)applied during a Reset build (a Continue keeps the
// frozen DecoDefault filler items), and BlockKey change-detection has no deco dimension, so nothing
// self-heals a deco-template edit — the editor returned 200 but built filler stayed stale until a
// manual Reset (#251). Enqueue a Reset for every playout that references this deco template.
// Post-commit enqueue uses CancellationToken.None so a late cancellation can't drop the rebuild after
// the data has already changed (audit #22 policy).
// manual Reset (#251). Enqueue a Reset for every playout that references this deco template. This
// whole post-commit invalidation runs with CancellationToken.None (audit #22 policy): once the edit
// is committed, a late request cancellation must not be able to abort the affected-playout query OR
// the enqueue and leave content stale.
List<int> playoutIds = await dbContext.PlayoutTemplates
.Where(pt => pt.DecoTemplateId == decoTemplate.Id)
.Select(pt => pt.PlayoutId)
.Distinct()
.ToListAsync(cancellationToken);
.ToListAsync(CancellationToken.None);
foreach (int playoutId in playoutIds)
{
@@ -185,23 +185,25 @@ public class UpdateDecoHandler(
// Deco content (break/default filler/etc.) is only (re)applied during a Reset build, and BlockKey
// change-detection has no deco dimension, so editing a deco left already-built filler/break content
// stale until a manual Reset (#251). Enqueue a Reset for every playout that references this deco —
// directly (Playout.DecoId) or through a deco template that includes it. Post-commit enqueue uses
// CancellationToken.None so a late cancellation can't drop the rebuild (audit #22 policy).
// directly (Playout.DecoId) or through a deco template that includes it. This whole post-commit
// invalidation runs with CancellationToken.None (audit #22 policy): once the edit is committed, a
// late request cancellation must not be able to abort either the affected-playout queries OR the
// enqueue and leave content stale — the entire side effect has to complete.
List<int> directPlayoutIds = await dbContext.Playouts
.Where(p => p.DecoId == request.DecoId)
.Select(p => p.Id)
.ToListAsync(cancellationToken);
.ToListAsync(CancellationToken.None);
List<int> decoTemplateIds = await dbContext.DecoTemplateItems
.Where(i => i.DecoId == request.DecoId)
.Select(i => i.DecoTemplateId)
.Distinct()
.ToListAsync(cancellationToken);
.ToListAsync(CancellationToken.None);
List<int> templatePlayoutIds = await dbContext.PlayoutTemplates
.Where(pt => pt.DecoTemplateId.HasValue && decoTemplateIds.Contains(pt.DecoTemplateId.Value))
.Select(pt => pt.PlayoutId)
.ToListAsync(cancellationToken);
.ToListAsync(CancellationToken.None);
foreach (int playoutId in directPlayoutIds.Concat(templatePlayoutIds).Distinct())
{
@@ -18,17 +18,22 @@ public class UpdateDefaultDecoHandler(
{
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
await dbContext.Playouts
int updated = await dbContext.Playouts
.Where(p => p.Id == request.PlayoutId)
.ExecuteUpdateAsync(u => u.SetProperty(p => p.DecoId, p => request.DecoId), cancellationToken);
// Changing a playout's default deco only takes effect on a Reset build (deco content is applied
// during Reset, and BlockKey has no deco dimension to self-heal) — same stale-until-Reset class
// as #251. Enqueue a Reset for the affected playout. CancellationToken.None so the rebuild isn't
// dropped after the assignment has already been persisted.
await channel.WriteAsync(
new BuildPlayout(request.PlayoutId, PlayoutBuildMode.Reset),
CancellationToken.None);
// as #251. Enqueue a Reset for the affected playout, but only if a row was actually updated: a
// request for a nonexistent playout must not queue a build for an id that isn't there (keeps the
// enqueued set equal to the affected set). CancellationToken.None so the rebuild isn't dropped
// after the assignment has already been persisted.
if (updated > 0)
{
await channel.WriteAsync(
new BuildPlayout(request.PlayoutId, PlayoutBuildMode.Reset),
CancellationToken.None);
}
return Option<BaseError>.None;
}
@@ -144,6 +144,17 @@ public class DecoInvalidationTests
reset.ShouldAllBe(bp => bp.Mode == PlayoutBuildMode.Reset);
}
[Test]
public async Task UpdateDefaultDeco_Should_Not_Enqueue_For_A_Nonexistent_Playout()
{
// no playout seeded — ExecuteUpdateAsync matches 0 rows
var handler = new UpdateDefaultDecoHandler(_db.Factory, _channel.Writer);
Option<BaseError> result = await handler.Handle(new UpdateDefaultDeco(PlayoutId: 999, DecoId: 1), CancellationToken.None);
result.IsNone.ShouldBeTrue();
DrainResetEnqueues().ShouldBeEmpty(); // enqueued set must equal the affected (empty) set
}
private static PlayoutTemplate MakePlayoutTemplate(int playoutId, int? decoTemplateId) =>
new()
{