Changing scene while worker threads are running

Godot Version

4.4 stable

Question

As part of the save loading system for my game Fortifend, I’m calling change_scene_to_file() on the SceneTree to initialize a freshly loaded map (before injecting saved game state data). Several pathfinding worker threads are part of the map scene, manged by mutexes and semaphores also in that scene.

How worried should I be about closing these worker threads “properly” in the old scene? I get an ominous warning, but I don’t know if it really matters:

W 0:00:44:231 ~Thread: A Thread object is being destroyed without its completion having been realized.
Please call wait_to_finish() on it to ensure correct cleanup.

W 0:00:44:231 ~Semaphore: A Semaphore object is being destroyed while one or more threads are still waiting on it.
Please call post() on it as necessary to prevent such a situation and so ensure correct cleanup.

Because my pathfinding is a performance critical “tight loop” component, I’m wary about checking a shutdown flag variable every step to see if the thread should exit.

If there are semaphores involved and you kill the thread prematurely you could end up in a deadlock situation.

If the complex computation can be done in smaller chunks you could use a flag to tell the thread to cancel its work and exit faster, and wait for the thread to finish.

I haven’t noticed any problems from just killing the entire scene (which happens automatically when I switch to a different scene). The Thread objects as well as all associated mutexes and semaphores are all defined in the same script, so presumably they’re all deallocated at the same time without a problem. My main concern was that there would be some sort of zombie threads hanging around after the rest of the scene is gone.

Maybe hang them off a global node/script where the game can be a little lazier about managing them?