Circuits: prevent cyclical parent dependency (#24882)

This commit is contained in:
andig 2025-10-31 10:56:55 +01:00 • committed by GitHub
parent d000a3863d
commit d818bf1467
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 0 deletions

View file

@ -148,6 +148,12 @@ func (c *Circuit) GetParent() api.Circuit {
// setParent set parent circuit
func (c *Circuit) setParent(parent api.Circuit) error {
// prevent cyclical dependency
for p := parent.GetParent(); p != nil; p = p.GetParent() {
if c == p {
return fmt.Errorf("cycle detected: %s and %s cannot be mutual parents", c.GetTitle(), parent.GetTitle())
}
}
c.mu.Lock()
defer c.mu.Unlock()
if c.parent != nil {

View file

@ -139,3 +139,13 @@ func TestCircuitCurrents(t *testing.T) {
ctrl.Finish()
}
}
func TestWrapCycleDetection(t *testing.T) {
log := util.NewLogger("foo")
pc, _ := New(log, "root", 0, 0, nil, 0)
lpc, _ := New(log, "lpc", 0, 0, nil, 0)
require.NoError(t, lpc.setParent(pc))
require.Error(t, pc.Wrap(lpc))
}