Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Game does not exit when non-daemon thread is running in the JVM #566

Closed
MartinHaeusler opened this issue Jan 24, 2024 · 1 comment
Closed
Assignees
Labels
bug Something isn't working topic:core

Comments

@MartinHaeusler
Copy link
Contributor

If we create a regular thread in Kotlin which keeps running, like so:

@RegisterClass
class MyButton : Button() {

    @RegisterFunction
    override fun _pressed() {
        thread {
            GD.print("Sleeping")
            Thread.sleep(TimeUnit.HOURS.toMillis(1))
            GD.print("Wake up!")
        }

    }
}

... then the game window will not exit properly when clicking on the window close ("x") button. Hitting the "stop" button in the editor still works.

The workaround is to declare the thread as a daemon thread:

    thread(daemon = true){
        // ...
    }

... as this will not prevent the JVM from exiting.

I don't think that this needs to have super high priority, but if a library would spawn threads (e.g. an async HTTP client might do that) and doesn't permit control over the creation of the thread, then this can become an issue.

Maybe this can be handled on the C++ side? It works when stopping the game via the godot editor "stop" button after all.

@CedNaru
Copy link
Member

CedNaru commented Aug 31, 2024

We added a new callback system in #661. You can now use a new GD.callWhenClosing() method to register a lambda that will be run when the JVM ends. That should be used to close any third party threads, or at least terminate the library using even if you don't have direct control over it. The JVM won't close anyway if a non daemon thread is still alive, if somehow it doesn't allow that, it's a more general Kotlin/Java issue not related to Godot.

@CedNaru CedNaru closed this as completed Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working topic:core
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants