How to kill the program from within itself?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Merlin1846

I would like to kill the program or terminate it from within the program similar to the get_tree().quit() function but without waiting for the iteration to finish or return anything.

there is OS.kill() method, I never tried using it though

Inces | 2022-01-07 09:40

:bust_in_silhouette: Reply From: Hugo4IT

Simple answer: D O N T

Killing your game without finishing anything leaves possibly hundreds of megabytes of ram allocated, possibly even corrupting your save file if you happen to have auto-save running while killing the game.

The reason it isn’t quitting is because you’re performing some expesive operation, or running a for-loop with many iterations. The only viable solution is to make the loop check for a quit signal.

An alternative solution

For example, if you have a for-loop with a lot of iterations (e.g. for generating a procedural world), you can do either of two things: Multithreading (the hard way), or use coroutines, this example shows yield being used to wait a frame for every iteration so Godot can check for inputs or the quit signal, but I recommend to look into yield and come up with a better solution, as each iteration will take a minimum of one frame this way (1/60th of a second):

extends Node2D

func long_loop() -> void:
	for i in range(999):
		print(i) # Example
		yield(get_tree(), "idle_frame") # Allow Godot to update

func _ready() -> void:
	get_node("Button").connect("pressed", self, "button_pressed")
	long_loop()

func button_pressed() -> void:
	get_tree().quit()

Killing your game without finishing anything leaves possibly hundreds of megabytes of ram allocated

Processes don’t work like that on modern operating systems. The OS will be able to reclaim memory after the process is abruptly killed.

The part about potential savefile corruption is true though.

Calinou | 2022-01-09 23:38

Like Calinou said a modern OS can just reclaim this I’ve seen this happen on my 21.04 Ubuntu after running a program with a massive memory leak (that an unknown person who is not me, made). I recently got my first Windows copy and even it has a system to take care of that. As for Mac, I have never used a Mac.

Merlin1846 | 2022-01-10 14:52

:bust_in_silhouette: Reply From: Calinou

OS.kill(OS.get_process_id()) should do the trick. Beware, there is no way to catch this from your code (which means you can’t ensure files are safely written before killing the process)!

Also, if you call OS.kill() after an infinite loop was started, the OS.kill() line will never be reached, which means your process won’t be killed.

Do you know? Will multi-threading still allow the OS.kill command to run? I’m asking this because the program is less of a game and more of a massive algorithm that could be put in a game but is quite slow even on my Ryzen 5500U and multi-threading would speed it up a lot since I have 12 cores. Or would that not allow the OS.kill() call to happen? Because having a thread that just kills the program after it has been running for more than a certain amount of time without the main algorithm finishing would be really nice I think.

Merlin1846 | 2022-01-10 14:57

Using multiple threads within your project should not impact OS.kill()'s ability to work. I’m not sure if OS.kill() will work if it’s called within a non-main thread – it logically should, but please test this just in case.

Calinou | 2022-01-21 01:05