InvalidOperationException : Graphs cannot be added, removed or serialized while the graph structure is locked

I am getting this in production, it does not happen in editor or at least I haven’t seen it. Build is mobile.

Non-fatal Exception: java.lang.Exception: InvalidOperationException : Graphs cannot be added, removed or serialized while the graph structure is locked. This is the case when a graph is currently being scanned and when executing graph updates and work items.
However as a special case, graphs can be added inside work items.
       at Pathfinding.AstarData.AssertSafe(Pathfinding.AstarData)
       at Pathfinding.AstarData.ClearGraphsInternal(Pathfinding.AstarData)
       at AstarPath.OnDestroy(AstarPath)

Not sure what it means but I am not doing anything out of the ordinary, but just to be sure I am looking at “AstarPath.active.isScanning” before I tell it to scan.

Is it possible that you are destroying the AstarPath component (e.g. by loading a new scene) while you are also scanning a graph asynchronously?

that makes sense, I’ll try that and will update this thread if needed.
EDIT: although the async scanning is in a coroutine, so is this even possible? the gameobject would die with the scene and the graph

Sure. As long as the coroutine started to scan the graph asynchronously, then the graphs will be locked. Once you’ve started scanning the graph asynchronously, you must complete it (not doing so risks memory leaks).

what if I started scanning, then for some reason the scene gets unloaded, what should I do at this point?

If you store the coroutine in your script, you can step through it to the end in a single frame before you unload the scene.

this is the code I’m using to scan, should I do anything different?

			if (!IsAstarCached && !AstarPath.active.isScanning)
				foreach (Progress _ in AstarPath.active.ScanAsync())
				{
					yield return new WaitForEndOfFrame();
				}

This is inside a coroutine