diff --git a/src/HtmlAgilityPack.Shared/HtmlNode.cs b/src/HtmlAgilityPack.Shared/HtmlNode.cs
index b4dfce7..4eae22f 100644
--- a/src/HtmlAgilityPack.Shared/HtmlNode.cs
+++ b/src/HtmlAgilityPack.Shared/HtmlNode.cs
@@ -1005,6 +1005,12 @@ public void SetChildNodesId(HtmlNode chilNode)
foreach (HtmlNode child in chilNode.ChildNodes)
{
_ownerdocument.SetIdForNode(child, child.GetId());
+
+ if (child.ChildNodes == chilNode.ChildNodes)
+ {
+ throw new Exception("Oops! a scenario that will cause a Stack Overflow has been found. See the following issue for an example: https://github.com/zzzprojects/html-agility-pack/issues/513");
+ }
+
SetChildNodesId(child);
}
}