Skip to content

Fix infinite recursion in AutoLayout for empty graphs#25

Merged
Malcolmnixon merged 1 commit into
mainfrom
fix/auto-empty-graph-infinite-recursion
Jul 16, 2026
Merged

Fix infinite recursion in AutoLayout for empty graphs#25
Malcolmnixon merged 1 commit into
mainfrom
fix/auto-empty-graph-infinite-recursion

Conversation

@Malcolmnixon

Copy link
Copy Markdown
Member

This pull request addresses a critical stack overflow issue that occurred when applying the AutoLayoutAlgorithm to an empty graph with specific algorithm settings. The main changes ensure that self-referential algorithm resolution does not cause infinite recursion, and new tests are added to verify this behavior.

Bug fix: Prevent infinite recursion on empty graphs

  • Updated AutoLayoutAlgorithm.ApplyCore to detect when the effective algorithm ID is self-referential (either "auto" or "hierarchical") for empty graphs, and to directly delegate to the layered algorithm instead of risking infinite recursion. This prevents a stack overflow when an empty graph explicitly sets its algorithm to "auto" or "hierarchical".

Testing: Added regression tests for empty graph algorithm resolution

  • Added two unit tests in AutoLayoutAlgorithmTests.cs:
    • Apply_EmptyGraph_ExplicitAutoAlgorithm_ReturnsEmptyTree ensures that an empty graph with algorithm "auto" returns an empty tree without recursion.
    • Apply_EmptyGraph_ExplicitHierarchicalAlgorithm_ReturnsEmptyTree ensures that an empty graph with algorithm "hierarchical" also avoids recursion and returns an empty tree.

…archical algorithm

AutoLayoutAlgorithm.ApplyCore's empty-graph (count == 0) fast path
unconditionally delegated to the internal HierarchicalLayoutAlgorithm
instance. That instance's own registry resolves both 'auto' and
'hierarchical' back to this same AutoLayoutAlgorithm instance (needed
so nested 'auto' scopes re-evaluate correctly). Since an empty graph
can never contain a container, HierarchicalLayoutAlgorithm.LayoutScope
always takes its flat fast path and calls straight back into this same
method with the same empty graph whenever the graph or options declare
Algorithm as 'auto' or 'hierarchical' - infinite mutual recursion that
overflows the stack (confirmed by reproduction: process exit code
0xC00000FD, STATUS_STACK_OVERFLOW).

Fix: resolve the cascaded effective algorithm id directly in the
empty-graph fast path, and short-circuit straight to the default leaf
(layered) whenever it is self-referential, before ever consulting
_hierarchical. Every other id still routes through
_hierarchical.ApplyCore exactly as before, preserving existing
resolution (and resolution-error) behavior unchanged.

Added regression tests proving an empty graph with Algorithm explicitly
set to 'auto' or 'hierarchical' resolves without crashing.
@Malcolmnixon Malcolmnixon added the bug Something isn't working label Jul 16, 2026
@Malcolmnixon
Malcolmnixon merged commit 9dc41bb into main Jul 16, 2026
6 checks passed
@Malcolmnixon
Malcolmnixon deleted the fix/auto-empty-graph-infinite-recursion branch July 16, 2026 05:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant