godotengine / godot

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

VisualScript nodes are self-referential #61138

Open derammo opened 2 years ago

derammo commented 2 years ago

Godot version

v4.0.alpha.calinou.067c1eb92

System information

Windows 10

Issue description

VisualScript resources have references to themselves. Nodes of Function type try to resolve the script from which the function comes and look that script resource up in the ResourceCache. However, this script is itself in the usual case, leading to an indirect dependency on itself.

This causes:

Resources involved in the cycle are never cleaned up.

Resources can't be serialized without the ResourceCache catching the dependency of the Resource on itself. This should be handled in the Resource, which should correctly handle being serialized or deserialized regardless of whether the cache is used.

Steps to reproduce

How to

image

Problem 1

On exit:

Leaked instance: VisualScriptSwitch:-9178347035697301922 - Resource path: res://Control.vs::VisualScriptSwitch_bu1cq
Leaked instance: VisualScript:-9178346485941488033 - Resource path: res://Control.vs
Hint: Leaked instances typically happen when nodes are removed from the scene tree (with `remove_child()`) but not freed (with `free()` or `queue_free()`).
ERROR: Resources still in use at exit (run with --verbose for details).
   at: clear (core/io/resource.cpp:479)
Resource still in use: res://Control.vs::VisualScriptSwitch_bu1cq (VisualScriptSwitch)
Resource still in use: res://Control.vs (VisualScript)
Orphan StringName: Control
Orphan StringName: VisualScript
Orphan StringName: ports_changed
Orphan StringName: VisualScriptSwitch
StringName: 4 unclaimed string names at exit.

Problem 2

This problem does not occur in the product today but can be used to easily see the dependency on itself.

Load this resulting *.vs resource with Resource.load(... CACHE_MODE_IGNORE)

This will lead to infinite recursion, and Godot will crash hard with stack limit exceeded, or number of open file handles exceeded, whichever comes first.

Notes

This issue was discussed on #scripting in chat and requested by @theoway

Minimal reproduction project

No response

Hakunamawatta commented 3 months ago

This issue should be closed. VisualScript is dead.

AThousandShips commented 3 months ago

VisualScript is still a thing in 3.x, so unless this isn't an issue in the 3.x version of VisualScript this is still relevant