Apply Fix 2: evict ModelCache on game exit, flush pending callbacks

Adds evict_all() to ModelCache which sets _shutting_down before flushing
_pending with null callbacks, then clears _mem and _job_queue. This
prevents in-flight HTTP completions from firing into a freed game scene
and ensures each new game starts with a clean WASM heap. Called from
game_screen._exit_tree() after all signal disconnections.

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-05-09 19:18:10 -04:00
co-authored by Claude Happy
parent 30a4211276
commit b926957329
2 changed files with 26 additions and 0 deletions
@@ -92,6 +92,31 @@ func evict(url: String, etag: String = "") -> void:
DirAccess.remove_absolute(disk)
## Drop all in-memory PackedScene references and flush the pending queue.
## Sets _shutting_down first so any in-flight HTTP callbacks that complete
## after scene teardown return null instead of firing into freed nodes.
## Disk cache (.bin files) is intentionally left intact so subsequent
## sessions still benefit from skipping re-downloads.
func evict_all() -> void:
_shutting_down = true
# Flush all queued callbacks with null so callers don't hang.
for key: String in _pending:
for cb: Callable in _pending[key]:
cb.call(null)
_pending.clear()
_mem.clear()
_job_queue.clear()
# _active_jobs is intentionally left alone; in-flight HTTPRequest nodes own
# those slots and will call _finish_job() when complete (draining an empty queue).
# Re-arm immediately so load_model() works for the next game.
_shutting_down = false
if OS.is_debug_build():
print("ModelCache: evict_all complete")
## Request a model by CDN URL.
## callback(node: Node3D) is called on the main thread; node may be null.
## If the same URL+ETag is already downloading, callbacks are queued and all
@@ -377,6 +377,7 @@ func _exit_tree() -> void:
_reset_unit_hover_glow()
Chat.set_bubble_host(null)
Chat.release_bubble_visibility(self)
ModelCache.evict_all()
# ── State → scene orchestration ───────────────────────────────────────────────