TheAlgorithms/C-Sharp · error · PathfindingException
Detected the same node twice. Confusion how this could ever…
Error message
Detected the same node twice. Confusion how this could ever happen
What it means
AStar's AddOrUpdateConnected throws PathfindingException when it encounters a neighbor node that is neither new nor already tracked in its open/closed sets — i.e. the same node appears twice where the algorithm's bookkeeping expects exactly one entry. This indicates corrupted or inconsistent node/state data rather than a normal pathfinding condition.
Solutions
- Fix node Equals/GetHashCode so equal nodes hash identically.
- Deduplicate the graph's node/neighbor lists before running A*.
- Ensure node identity fields are immutable during the search.
Example fix
// before public override bool Equals(object o) => Id == ((Node)o).Id; // hashCode not overridden // after public override bool Equals(object o) => o is Node n && Id == n.Id; public override int GetHashCode() => Id.GetHashCode();
Defensive patterns
Strategy: try-catch
Validate before calling
var distinct = graph.Nodes.Distinct().ToList();
if (distinct.Count != graph.Nodes.Count) throw new InvalidOperationException("Duplicate nodes in graph"); Type guard
static bool HasConsistentIdentity<T>(IEnumerable<T> nodes) where T : class =>
nodes.All(n => n.GetHashCode() != 0) && nodes.Distinct().Count() == nodes.Count(); Try / catch
try { var path = AStar.Compute(start, goal); }
catch (PathfindingException ex) { logger.LogError(ex, "A* node bookkeeping violated; check Equals/GetHashCode"); } Prevention
- Always override GetHashCode when overriding Equals on nodes
- Keep node identity fields immutable during search
- Deduplicate neighbor lists when building the graph
When it happens
Trigger: A graph whose node equality/hashing is inconsistent (GetHashCode/Equals disagree), duplicate node objects with equal identity, or a neighbor list containing the same node twice so Compute processes it in conflicting states.
Common situations: Custom node classes overriding Equals but not GetHashCode (or vice versa); graph with duplicate nodes loaded from data; mutable node keys changing while A* runs.
Understand the failure class
Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.
Related errors
- Graph contains a negative weight cycle.
- Matrix must be symmetric!
- Graph must be undirected!
- Adjacency matrix must be square!
- Adjacency matrix must be symmetric!
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/9bb63dbe8a9cb851.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Search/AStar/AStar.cs:135
connected.EstimatedCost = connected.CurrentCost + connected.DistanceTo(to);
connected.State = NodeState.Open;
queue.Enqueue(connected);
}
else if (current != connected)
{
// Updating the cost of the node if the current way is cheaper than the previous
var newCCost = current.CurrentCost + current.DistanceTo(connected);
var newTCost = newCCost + current.EstimatedCost;
if (newTCost < connected.TotalCost)
{
connected.Parent = current;
connected.CurrentCost = newCCost;
}
}
else
{
// Codacy made me do it.
throw new PathfindingException(
"Detected the same node twice. Confusion how this could ever happen");
}
}
}
}
View on GitHub (pinned to 96e2905cab)