godotengine / godotengine/godot-docs

Document that pending coroutines do not keep their object alive

Open
#11,182 0 comments 1 reaction 0 assignees View on GitHub
area:manual topic:gdscript
Dominant language
reStructuredText
Stars
5.7k
Forks
3.8k
Avg merge
1d 20h
Merged PRs (30d)
25

Description

**Your Godot version:** 4.5-beta3

**Issue description:**

If an object is destroyed before a coroutine finishes running, the coroutine is aborted and destroyed as well. This can cause surprises:

```gdscript
extends Node

func _ready():
# This prints "BLIP".
await TestClass.new().do_thing()
# This prints nothing, because the object is destroyed immediately after this line,
# and this destroys the coroutine as well.
TestClass.new().do_thing()

class TestClass:
signal blip
func do_thing():
blip.emit.call_deferred()
await blip
print("BLIP")
```

I think two things should be documented:

1. A pending coroutine does not keep its owning object alive.
2. Destroying an object destroys its pending coroutines as well.

**URL to the documentation page (if already existing):**

https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html#awaiting-signals-or-coroutines

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.