GDScript scripts that reference themselves leak at exit (GDScriptLanguage::finish stops early)

Abierto
#1,545 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
30/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
cpp, godot
Área
backend, testing

Línea de trabajo

Read GDScriptLanguage::finish() and GDScript::clear() in the engine source, then compare Redot’s clear() with the linked Godot fix and Redot pull request #8. Reproduce the shutdown leak with the attached MRP and run the unit suite. Done when the leak no longer appears for the repro and the unit suite passes.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

bug confirmed topic:gdscript usability
Tested versions
  • Reproducible in: v26.3.stable.official [85e520394]
  • Also reproducible in an unmodified local build of the same commit.
  • Upstream Godot fixed the same bug for 4.7 in godotengine/godot#117991 (issue godotengine/godot#117975). Redot does not have that change yet.
System information

Windows 10 Pro 22H2 (build 19045), x86-64. Reproduced headless (--headless --audio-driver Dummy), so the GPU and renderer are not involved.

Issue description

A project can print these at exit even though it does nothing wrong:

WARNING: ObjectDB instances leaked at exit (run with --verbose for details).
ERROR: 1 resources still in use at exit (run with --verbose for details).

With --verbose the resource is a GDScript, for example Resource still in use: res://holder.gd (GDScript).

Which scripts leak. The leaked script references itself:

  • a class that names itself in its own code (return Holder.new(), or Holder.g() inside class_name Holder);
  • or a class that reads its own static var from a function.

Why it comes and goes. It depends on load order, so the leak appears and disappears as unrelated code changes. In a game project it looked like random leaks after each refactor.

Cause. At shutdown, GDScriptLanguage::finish() walks script_list and calls clear() on each script to break those self-references. Each iteration:

  1. holds the current script in a local Ref;
  2. clears it;
  3. moves to the next list node;
  4. and only then releases the Ref.

If that Ref was the last one, the script is freed together with its base script. New scripts are added at the front of the list, so a base loaded just before its subclass is exactly the next node. The loop then continues from a freed node, which is a use-after-free. On Windows it usually just stops early, so every script after that point is never cleared and the self-referencing ones leak.

Godot found the same use-after-free with ThreadSanitizer (godotengine/godot#117975) and fixed it in godotengine/godot#117991 by releasing the base script inside GDScript::clear().

Fix available. A Redot version of that fix, for 26.3-stable, is in dominicbytes/redot-engine#8 (single commit e45ed70). Redot's clear() still uses the older ClearData code, so the change goes in the root branch, and it also clears the raw _base pointer that Godot no longer has.

Results:

  • This repro: leaks in 5 of 5 runs on the official build, and 0 of 10 with the fix.
  • Unit tests: the full suite passes with the fix, 1346 of 1346.
Steps to reproduce
  1. Create the MRP below.
  2. Import it once: redot --headless --path <mrp> --import.
  3. Run it: redot --headless --audio-driver Dummy --path <mrp>. The main scene quits in _ready().
  4. The leak warning and error print at exit on every run. Add --verbose to see res://holder.gd named.
Minimal reproduction project (MRP)

Bugsquad edit:

[!NOTE]
The order in which main.gd names Screen and then Confirm matters; swapping it, or making Confirm use new() instead of Confirm.new(), hides the leak.

gdscript-shutdown-leak-mrp.zip


Lenguaje dominante
C++
Estrellas
6.1k
Forks
337
Merge medio
5 d 8 h
PR fusionados (30 d)
13

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de Redot-Engine/redot-engine

Todos los issues de Redot-Engine/redot-engine

Issues similares

Más issues de C++

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.