Funkiness with signals and threads - bug or?

Godot Version

4.2.1

Question

I am observing some unexpected behavior when emitting a signal from a thread. It seems that even if a listener is connected to the signal before the thread is started, the listener appears to not be connected to the signal when the signal is invoked from the thread.

extends Node2D

signal thread_work_done

func _ready():
	print("ready")
	thread_work_done.connect(_on_thread_work_done)
	var t = Thread.new()
	t.start(thread_worker_func)

func _on_thread_work_done():
	print("thread_work_done listener invoked")
	

func thread_worker_func():
	print("thread work start")
	thread_work_done.emit()
	print("thread work signal emitted (listeners: %s) " % [thread_work_done.get_connections()])

In this example, the listener function is never invoked, and the final print statement shows that it is not yet connected.

I don’t know for sure but you are putting the thread on the stack, the ref could be removed(out of scope), so the signal is actually gone and not registered.

Try making it a field instead. (no idea about the thread issue if it’s not this)

Just a guess.