detect cycles in yaml sequence definitions (#2060)

This commit is contained in:
Jason Dove
2025-06-23 02:55:35 +00:00
committed by GitHub
parent b90463e3af
commit c4c164df6a
3 changed files with 31 additions and 4 deletions
+1 -1
View File
@@ -36,7 +36,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- This makes it obvious when hardware acceleration will not work as configured
- Add button in schedule editor to clone schedule item
- Allow YAML playout sequence definitions to reference other sequences
- Playout builder will behave in unexpected ways if nesting is too deep
- Cycles will be detected and logged, and sequences with cycles will prevent the playout from building
- Add `repeat` property to YAML sequence instruction
- This tells the playout builder how many times this sequence should repeat
- Omitting this value is the same as setting it to `1`
@@ -6,6 +6,7 @@ using ErsatzTV.Core.Interfaces.Repositories;
using ErsatzTV.Core.Interfaces.Scheduling;
using ErsatzTV.Core.Scheduling.YamlScheduling.Handlers;
using ErsatzTV.Core.Scheduling.YamlScheduling.Models;
using ErsatzTV.Core.Search;
using Microsoft.Extensions.Logging;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
@@ -127,10 +128,16 @@ public class YamlPlayoutBuilder(
}
}
int flattenCount = 0;
if (DetectCycle(context.Definition))
{
logger.LogError("YAML sequence contains a cycle; unable to build playout");
return playout;
}
var flattenCount = 0;
while (context.Definition.Playout.Any(x => x is YamlPlayoutSequenceInstruction))
{
if (flattenCount > 10)
if (flattenCount > 100)
{
logger.LogError(
"YAML playout definition contains sequence nesting that is too deep; this introduces undefined behavior");
@@ -197,6 +204,21 @@ public class YamlPlayoutBuilder(
return playout;
}
private static bool DetectCycle(YamlPlayoutDefinition definition)
{
var graph = new AdjGraph();
foreach (YamlPlayoutSequenceItem sequence in definition.Sequence)
{
foreach (YamlPlayoutSequenceInstruction instruction in sequence.Items.OfType<YamlPlayoutSequenceInstruction>())
{
graph.AddEdge(sequence.Key, instruction.Sequence);
}
}
return graph.HasAnyCycle();
}
private async Task<int> GetDaysToBuild() =>
await configElementRepository
.GetValue<int>(ConfigElementKey.PlayoutDaysToBuild)
@@ -1,4 +1,4 @@
namespace ErsatzTV.Infrastructure.Search;
namespace ErsatzTV.Core.Search;
public class AdjGraph
{
@@ -21,6 +21,11 @@ public class AdjGraph
return HasCycleImpl(from.ToLowerInvariant(), visited, stack);
}
public bool HasAnyCycle()
{
return _edges.Any(edge => HasCycle(edge.From));
}
private bool HasCycleImpl(string node, ISet<string> visited, ISet<string> stack)
{
if (stack.Contains(node))