Correct way to stop path finding thread?

When I load another scene , (in runtime) I get the following error :

Could not terminate pathfinding thread[0] in 50ms, trying Thread.Abort
AstarPath:OnDestroy() (at Assets/AstarPathfindingProject/Core/AstarPath.cs:1641)

I think I am not destroying / closing path finding thread correctly before I load the scene.

What is proper way to do this?

gone fishing? :smiley:

How to handle this issue?

Aron, if you don’t mind share some tips on how top stop the path finding thead before I load the scene so I don’t get the above errors?

I think there’s some underlying problem there. I tried modifying the code to wait for 5000ms for each thread and I’m still getting this error (plus it takes forever to exit play mode since it waits 5000ms for each pathfinding thread).

I’m getting a random number of threads with this issue each time I run it, usually 5 or 6 and different numbers each time:

Could not terminate pathfinding thread[0] in 50ms, trying Thread.Abort

(the thread[0] can be any of thread[1] through thread[7] in my case)

Any ideas on how to fix this? I’ve commented that line out for now but it always feels wrong to terminate threads like that…

Any ideas on how to fix this? I've commented that line out for now but it always feels wrong to terminate threads like that...

Yes I totally agree. I am doing exactly the same thing. Just commenting it out at the moment… but I am pretty sure that can’t be the “solution”

I guess Aron is still very busy…


Sorry for the late answer. I have had (and still have to some extent) a lot to do the last few weeks.
Yeah I think there is a bug at the moment where the pathfinding thread refuses to terminate gracefully, so that fallback code is executed. It shouldn’t be a problem for your games however, it’s just an annoying error message.
I will try to fix it for the next version.

excellent. good to hear.


You can call Interrupt just before the call to Join. It will inform the other thread to exit via an exception. You’ll need to add a check in CalculatePathsThreaded for that exception so it does not trigger another error message. Calling Join by itself does not necessarily let the other thread know it should exit which is why it was timing out for me.

There are other ways to handle this, but it is how I avoided that error message.

In file AstarPath.cs:
public void OnDestroy () { ... threads[ i ].Interrupt(); if (!threads[i].Join (50)) { ... }

private static void CalculatePathsThreaded (System.Object _threadInfo) { ... catch (System.Exception e) { if (e is System.Threading.ThreadInterruptedException) return; ...
Hopefully this helps.

Yeah, but I don’t really want to use an interrupt.
There are some other flags which the threads check for, and those should make it exit gracefully. That isn’t happening right now however.

How’s progress going on with this issue?

Nothing yet, but as said earlier, I will try to fix it before I release the next version.

Sorry to dig out an old thread, figured better than making a duplicate…

I am getting the same:
Could not terminate pathfinding thread[10] in 200ms, trying Thread.Abort
UnityEngine.StackTraceUtility:ExtractStackTrace ()
UnityEngine.DebugLogHandler:LogFormat (UnityEngine.LogType,UnityEngine.Object,string,object[])
UnityEngine.Logger:Log (UnityEngine.LogType,object)
UnityEngine.Debug:LogError (object)
Pathfinding.PathProcessor:JoinThreads () (at Packages/com.arongranberg.astar@4.3.51/Core/Misc/PathProcessor.cs:260)
AstarPath:ShutdownPathfindingThreads () (at Packages/com.arongranberg.astar@4.3.51/Core/AstarPath.cs:1405)
AstarPath:OnDisable () (at Packages/com.arongranberg.astar@4.3.51/Core/AstarPath.cs:1427)

Unfortunately, this is happening between each level. I can ignore it for a while, but seemingly eventually leads to Unity editor stalling, forcing me to end task restart. Obviously the game can’t crash between levels. Any ideas how to exit gracefully?


I am also getting the same error.

Could not terminate pathfinding thread[10] in 200ms, trying Thread.Abort

I am using Unity 2022.3.7f1 and Astar Pathfinding Project version 5.0.2.

The scene using the AstarPath component is the scene loaded with LoadSceneMode.Additive.
The error occurs when unloading this scene.

I have deactivated all objects that use AIPath before unloading the scene, but this error still occurs.
Is there any way to resolve this error?



Can you consistently replicate this error in the unity editor?

Dear Mr.aron,

Thanks for the reply.

It does not occur in the editor.
It occurs in Android.
My explanation was insufficient. Sorry about that.

The probability of occurrence is approximately 30%.
I forgot to explain that there are nearly 2,000 agents placed in the scene.

Do you know the hardware specs of this device? How many cores does it have?

If you set A* Inspector → Settings → Log Mode to “Normal”, how long do your paths normally take to calculate? Do some of them exceed 100ms?

The device used is a Goole Pixel 7a and Google Pixel8
The number of cores was 8.

Log Mode is set to Error Only.
AStarPath Thread Count is set to Automatic High Load.

error log:
“Could not terminate pathfinding thread[10] in 200ms, trying Thread.Abort”

This error log was sometimes output three to four times.

Alright. But if you set it to “Normal”, how long are the path calculation times, at most?