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

オープン
#1,545 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
30/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
cpp, godot
領域
backend, testing

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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


主要言語
C++
スター
6.1k
フォーク
337
平均マージ
5日 8時間
マージ済み PR(30日)
13

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

Redot-Engine/redot-engine のほかの issue

Redot-Engine/redot-engine の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。