For some strange reason in my project I am getting this error when trying to dispose of a thread I just created:
ERROR: A Thread can't wait for itself to finish.
at: Thread::wait_to_finish (core\os\thread.cpp:105) - Condition "id == get_caller_id()" is true.
Which doesn't make any sense because the thread in question never calls the function that creates it. I've tested it with calling print()
at the start of the function and can confirm it never gets called throughout running the thread.
I can't seem to replicate this issue but this is the code I am using:
AI_thread = Thread.new()
AI_thread.start(Global_Variables_3D.world.AI_Manager, "generate_loader_executors_for_all_inactive_entities_in_each_combat_controller_group")
#waiting for the result of this function
yield(Global_Variables_3D.world.AI_Manager,"ai_loader_executors_generated")
AI_thread.wait_to_finish() #error thrown here
ai_loader_executors = Global_Variables_3D.world.AI_Manager.ai_loader_executors
Upon further experimentation with OS.alert()
I've found that something really strange is going on:
AI_thread = Thread.new()
OS.alert("STRANGE0") #this halts the main thread
AI_thread.start(Global_Variables_3D.world.AI_Manager, "generate_loader_executors_for_all_inactive_entities_in_each_combat_controller_group")
OS.alert("STRANGE1") #this halts the main thread
#waiting for the result of this function
yield(Global_Variables_3D.world.AI_Manager,"ai_loader_executors_generated")
OS.alert("STRANGE2") #this doesn't halt the main thread!?!?
AI_thread.wait_to_finish()
ai_loader_executors = Global_Variables_3D.world.AI_Manager.ai_loader_executors
It's as if the yield()
call on a signal which originates from the thread's function causes the current function to become a part of the thread!
Just now while writing this I tried waiting for the thread to finish by using AI_thread.call_deferred("wait_to_finish")
instead and it worked to negate the error (edit: a better solution was to call the "ai_loader_executors_generated" signal within the thread with call_deferred
). Though I still can't replicate this really strange behavior at a smaller scale, this runs without the _ready()
function becoming a part of the thread and thus no error is thrown by wait_to_finish()
:
var threadboi
signal ammirite()
func thread_func():
yield(get_tree().create_timer(3),"timeout")
emit_signal("ammirite")
func _ready():
for i in range(2):
threadboi = Thread.new()
threadboi.start(self, "thread_func")
#yield until the thread is complete
yield(self,"ammirite")
threadboi.wait_to_finish()
print("done_ammirite_yield")
So I wonder what the cause of this behavior is if anyone knows how to replicate it.