godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
88.89k stars 20.15k forks source link

WorkerThreadPool doesn't call `ScriptServer::thread_exit()` #95809

Closed CedNaru closed 2 days ago

CedNaru commented 3 weeks ago

Tested versions

Godot 4.3 stable

System information

Windows 10

Issue description

We are upgrading our Kotlin language binding to Godot 4.3, but the engine doesn't want to close anymore. Before our embedded JVM can close, it waits for all "attached" threads to be closed. We usually attach/detach threads to the JVM using the callback ScriptServer::thread_enter / ScriptServer::thread_exit.

It seems that a recent change to WorkerThreadPool makes all threads call ScriptServer::thread_enter (in a delayed manner because the pool is initialized before the ScriptServer). The issue seems to be that ScriptServer::thread_exit is never called by it before the ScriptServer is terminated. Instead, it only happens when the pool threads are deleted as part of the WorkerThreadPool's destructor, which happens in the very last steps of Main::cleanup.
I also can't find any callbacks that would be executed this late either (MessageQueue is freed before the pool as well).

I'm not certain that this is the source of our issue but we confirmed that we received more thread_enter calls than thread_exitcalls. So it's either the pool, or some mysterious other thread somewhere in Godot that is not terminated in time.

@RandomShaper

Steps to reproduce

-

Minimal reproduction project (MRP)

-

AThousandShips commented 3 weeks ago

The enter was added in:

But without corresponding exit, wasn't discussed specifically there