godotengine / godotengine/godot

Printing in the same frame the window closes gives no output

Open
#90,667 6 comments 4 reactions 0 assignees View on GitHub
discussion topic:editor
Dominant language
C++
Stars
117k
Forks
26.8k
PR merge metrics
PR metrics pending

Description

### Tested versions

- Tested in: 4.2.2 rc2 and 4.2.2 rc3

### System information

Godot v4.2.2.rc3 - Windows 10.0.22631 - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 3050 6GB Laptop GPU (NVIDIA; 31.0.15.4601) - 13th Gen Intel(R) Core(TM) i5-13450HX (16 Threads)

### Issue description

If I try to print anything on the same frame the game window closes in the editor, nothing gets printed. For example, if I run a scene with the following script attached, I would expect to see "I'm exiting the tree" in the output console after pressing ESCAPE:

```gdscript
extends Node

func _process(_delta: float) -> void:
if Input.is_action_just_pressed("ui_cancel"):
print("I'm exiting the tree")
get_tree().quit()
```

However, upon testing, the output console is empty. If I add an `await get_tree().create_timer(0.1).timeout` statement after the print line, however, it prints to the output as expected:

```gdscript
extends Node

func _process(_delta: float) -> void:
if Input.is_action_just_pressed("ui_cancel"):
print("I'm exiting the tree")
await get_tree().create_timer(0.1).timeout
get_tree().quit()
```

It also works if I add a `breakpoint` in place of the `await get_tree().create_timer(0.1).timeout`, so it seems that Godot just doesn't like printing stuff in the same frame the window closes.

I have also tested using the `_exit_tree` function and the behaviour is the same. The following code does not print anything when I press ESCAPE:

```gdscript
extends Node

func _process(_delta: float) -> void:
if Input.is_action_just_pressed("ui_cancel"):
get_tree().quit()

func _exit_tree() -> void:
print("I'm exiting the tree")
```

But if I add a breakpoint, it works correctly:

```gdscript
extends Node

func _process(_delta: float) -> void:
if Input.is_action_just_pressed("ui_cancel"):
get_tree().quit()

func _exit_tree() -> void:
print("I'm exiting the tree")
breakpoint
```

### Steps to reproduce

1. Create a new scene with a root node of type `Node`;
2. Attach the following script to that node:
```gdscript
extends Node

func _process(_delta: float) -> void:
if Input.is_action_just_pressed("ui_cancel"):
print("I'm exiting the tree")
get_tree().quit()
```
3. Press play, then press the ESCAPE key;
4. Check the output console and verify that nothing got printed;
5. Change the script to the following:
```gdscript
extends Node

func _process(_delta: float) -> void:
if Input.is_action_just_pressed("ui_cancel"):
print("I'm exiting the tree")
await get_tree().create_timer(0.1).timeout
get_tree().quit()
```
6. Press play, then press the ESCAPE key;
7. Check the output console and verify that "I'm exiting the tree" was printed correctly.

### Minimal reproduction project (MRP)

[print-on-close-test.zip](https://github.com/godotengine/godot/files/14971212/print-on-close-test.zip)

Contributor guide

Open the contributing guide

Research direction

Start with the minimal reproduction project linked in the issue and follow the listed reproduction steps, comparing immediate printing with the delayed and breakpoint cases. The fix is done when output printed during tree shutdown is visible in the editor console without requiring a delay or breakpoint.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
game-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.