Bug - StackOverflow and Unhandled Exceptions

I just got these three errors:

StackOverflowException: The requested operation caused a stack overflow.
Pathfinding.TriangleMeshNode.UpdateRecursiveG (Pathfinding.Path path, Pathfinding.PathNode pathNode, Pathfinding.PathHandler handler) (at Assets/AstarPathfindingProject/Generators/NodeClasses/TriangleMeshNode.cs:143)
… [This keeps going into the same code, obviously, it’s a stack overflow :slight_smile: ]

Followed by :
Unhandled exception during pathfinding. Terminating.
UnityEngine.Debug:LogError(Object)
Pathfinding.PathProcessor:CalculatePathsThreaded(PathThreadInfo) (at Assets/AstarPathfindingProject/Core/Misc/PathProcessor.cs:363)
Pathfinding.c__AnonStorey70:<>m__22() (at Assets/AstarPathfindingProject/Core/Misc/PathProcessor.cs:95)

And :

Error : This part should never be reached.
UnityEngine.Debug:LogError(Object)
Pathfinding.PathProcessor:CalculatePathsThreaded(PathThreadInfo) (at Assets/AstarPathfindingProject/Core/Misc/PathProcessor.cs:369)
Pathfinding.c__AnonStorey70:<>m__22() (at Assets/AstarPathfindingProject/Core/Misc/PathProcessor.cs:95)

This happens with a modified RichAI, on a recast graph, searching for a multi target path that is modified a bit to add avoidance penalties for different spots.

I think this might be happening in other places as well. We were presenting our game at PSX over the weekend, and I’ve noticed some NPCs getting stuck and not moving. Now I see in editor that when this happens pathfinding for that agent will stop working completely.

Any ideas what might be going on?

Hi

That looks suspiciously like some penalty that has been changed in the middle of a path request. Is it possible that could happen with your code?

Best regards,
Aron Granberg

On the graph itself? I’m not changing any penalties as far as I know.

I only change tags on parts that get locked / unlocked, and updatePhysics at some places but never update use addPenalty field (although I have been meaning to make some change to some parts of the graph and add penalties at some places, but that would be done at awake so I should be safe there)

I use custom paths with GetTraversalCost() overriden to add penalties, could that be affecting the graph itself? Could it be that there is too much penalty added and this is the issue?

Here is the code:

public override uint GetTraversalCost(GraphNode node) { unchecked { //do this is XZ only long dx = (PlayerPosition.x - node.position.x); long dz = (PlayerPosition.z - node.position.z); long distSqr = dx * dx + dz * dz; if (distSqr < m_radiusSqrd) { //within avoidance radius, add a penalty scaled to how close we are to the radius return base.GetTraversalCost(node) + ((uint)(m_radiusSqrd - distSqr) * IN_RADIUS_PENALTY); } else return base.GetTraversalCost(node); }
    }

With updating penalties I mean anything that makes the GetTraversalCost method change its output during a single path request. Could the PlayerPosition change during the path request for example?

No. I store an Int3 of the position when constructing the path. I did however make a change that scales the value of the output based on the distance. so not every call will produce the same penalty. Could that be the issue?

Every call with the same node should produce the same penalty, otherwise bad things might happen.

Ok. So say 2 agents, in 2 different spot start a path. Both of them are trying to avoid the player.
Agent 1 start out searching, then agent 2 starts searching a second later. In the meanwhile, the player moves a bit. Will that break as well? or is the “Every call with the same node should produce the same penalty” is for just one path?

Also, I don’t think that is the case anyways. I cache the positions, so nothing really moves during a path search. each node just has its sqrDistance calculated and then compared to the radius from the position (which doesn’t change with during the path calculation).

It could be that rounding errors may have caused this since I started scaling the penalty. If the penalty is fixed, the above code/scenarios should work correctly?

Yes, for a single path.
Ok, so if the penalty is indeed fixed fixed for each node during a path request, then it might be something else. Are you overriding any other functionality of the path request? Does everything work fine if you remove your GetTraversalCost method?

The only other thing is that Construct() method is used.

The whole path is based on the GetTraversalCost(), so removing that means using the basic paths and not custom ones.

I’ve only seen this error once last night. I haven’t been able to reproduce again at all.