From b926957329574e340e46c72ec0fefb72431d2edb Mon Sep 17 00:00:00 2001 From: dingo Date: Sat, 9 May 2026 19:18:10 -0400 Subject: [PATCH] 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 Co-Authored-By: Happy --- Client/origin-client/autoloads/ModelCache.gd | 25 +++++++++++++++++++ .../origin-client/scenes/game/game_screen.gd | 1 + 2 files changed, 26 insertions(+) diff --git a/Client/origin-client/autoloads/ModelCache.gd b/Client/origin-client/autoloads/ModelCache.gd index f68b4f8..9fe02eb 100644 --- a/Client/origin-client/autoloads/ModelCache.gd +++ b/Client/origin-client/autoloads/ModelCache.gd @@ -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 diff --git a/Client/origin-client/scenes/game/game_screen.gd b/Client/origin-client/scenes/game/game_screen.gd index 715bbfe..1d16f5b 100644 --- a/Client/origin-client/scenes/game/game_screen.gd +++ b/Client/origin-client/scenes/game/game_screen.gd @@ -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 ───────────────────────────────────────────────