Milestone 22: Scene lifecycle and memory optimization, Part #4
Scene lifecycle and memory optimization
Summary
PR #852 – Scene lifecycle and memory optimization
Optimizes the browser-based Playwright E2E test suite for the Godot HTML5 export by improving scene lifecycle management, reducing resource usage, and increasing observability of Godot engine loading and gameplay flows.
Key Changes
Test infrastructure & lifecycle
- Introduces a module-scoped
shared_pagefixture so most tests reuse a single initialized Playwright page/context instead of reloading the Godot WASM engine per test. - Adds smarter
init_page_and_wait_readyhelper (usesdomcontentloaded+window.godotInitializedchecks; short-circuits when already initialized). - Stubs native
alert/confirmdialogs to prevent CDP hangs. - Adds per-test JS heap metrics capture and a consolidated lifecycle/memory summary at the end of the pytest run.
- Centralizes CDP/V8 coverage init/teardown with safe session detach (including for main-menu and audio tests).
- Improves process tracking so only test-spawned HTTP server / Chromium processes are terminated on session finish.
- Explicitly releases WebGL context on shared-page teardown to free GPU memory.
Local test server & CI hardening
- Upgrades the HTML5 test server to a threaded HTTP server with address reuse, stricter isolation headers, and improved signal trapping.
- Adds CI-specific Playwright Chromium launch arguments (software GL / ANGLE + SwiftShader, JS heap limits, no-sandbox, no
/dev/shm) to reduce GPU usage and OOM risk in containers.
Test refactoring
- Migrates the majority of E2E tests (audio, volume/mute, difficulty, navigation, fuel, weapon firing, reset flows, etc.) to the shared-page pattern.
- Standardizes navigation and menu flows via shared helpers (
open_options_menu,open_audio_menu,set_log_level, etc.). - Strengthens assertions with deterministic waits, state-persistence checks, and log-based validation while avoiding unnecessary page reloads.
- Retains function-scoped pages where early console/error capture before engine boot is required.
CI / Security
- Bumps the pinned
github/codeql-action/upload-sarifSHA in the Snyk and Trivy workflows for improved security and stability (scan behavior and upload logic otherwise unchanged).
Related
- Closes / addresses #773 – [TASK] Scene Lifecycle and Memory Optimization
- Supports Milestone 22: Optimize Test Suite Runtime & Fix Loading Screen
- Related epic: #771 – Optimize Test Suite Runtime and Prevent CI Limit Exhaustion
Impact
Reduces redundant Godot WASM boots, lowers memory/GPU pressure in CI and local runs, improves test reliability and observability, and hardens cleanup so orphaned processes and WebGL resources no longer leak across the suite.
Reviewer's Guide
Refactors Playwright E2E tests to reuse a shared browser page and centralized Godot initialization, adds per-test memory and process lifecycle tracking, hardens browser/server teardown for GPU and JS heap leaks, and updates CI browser flags and SARIF upload actions for stability and security.
File-Level Changes
| Change | Details | Files |
|---|---|---|
| Standardize E2E tests on a shared Playwright page fixture and centralized Godot initialization helper to reduce redundant engine boots and flakiness. |
|
tests/conftest.pytests/test_utils.pytests/reset_audio_flow_test.pytests/volume_sliders_mutes_test.pytests/difficulty_flow_test.pytests/audio_flow_test.pytests/navigation_to_audio_test.pytests/back_flow_test.pytests/fuel_depletion_test.pytests/difficulty_integration_test.pytests/weapon_firing_test.py |
| Improve test lifecycle observability and cleanliness with memory metrics and scoped listeners/coverage. |
|
tests/conftest.pytests/load_main_menu_test.pytests/validate_clean_load_test.pytests/no_error_logs_test.pytests/audio_flow_test.pytests/log_level_test.py |
| Refine high-level flow tests around audio, options, and gameplay to use shared helpers and stronger, less brittle assertions. |
|
tests/reset_audio_flow_test.pytests/volume_sliders_mutes_test.pytests/difficulty_flow_test.pytests/audio_flow_test.pytests/navigation_to_audio_test.pytests/back_flow_test.pytests/fuel_depletion_test.pytests/difficulty_integration_test.pytests/weapon_firing_test.pytests/test_utils.py |
| Harden browser and HTTP server behavior for CI and local runs to reduce OOM, GPU issues, and hanging servers. |
|
tests/conftest.pytests/ci/conftest.pyworkspace/run_browser_tests.sh |
| Update security scanning workflows to use newer pinned CodeQL SARIF upload actions. |
|
.github/workflows/snyk.yml.github/workflows/trivy.yml |
Assessment against linked issues
| Issue | Objective | Addressed | Explanation |
|---|---|---|---|
| #773 | Refactor Playwright browser, context, and page fixtures to reduce unnecessary browser launches and Godot WASM initialization cycles while preserving deterministic test isolation. | ✅ | |
| #773 | Improve lifecycle teardown and resource management (WebGL/canvas, Playwright contexts/pages, local HTTP server, and spawned processes) and add basic lifecycle/memory monitoring to prevent leaks during repeated E2E runs. | ✅ | |
| #773 | Update the specified web E2E tests to use the new shared lifecycle patterns (including lighter-weight navigation and reset mechanisms) without changing their functional assertions or coverage. | ✅ |
Possibly linked issues
- #[TASK] Scene Lifecycle and Memory Optimization: PR introduces shared Playwright fixtures, lifecycle metrics, teardown, and server/Chromium config changes matching the optimization task.
- #771: PR adds shared page lifecycle, memory metrics, HTTP server and CI Chromium optimizations, directly supporting the epic’s runtime and resource goals.
🤖 Bot & AI Contributions
This pull request includes substantial automated and AI-assisted contributions focused on dependency updates, code style enforcement, static analysis, and AI-powered code reviews/summaries.
Summary of Activity
- Dependency management: Automated bump of the GitHub CodeQL
upload-sarifaction SHA in Snyk and Trivy workflows for security and stability. - Code formatting & style: Multiple automated commits applying Black and isort formatting fixes across Python test files.
- AI code review & summarization: Generated PR summaries, reviewer’s guides, flow diagrams, and detailed review feedback on test lifecycle, shared page fixtures, cleanup patterns, and coverage assertions.
- Static analysis: Code quality / reliability review with PR report card (Security, Reliability, Complexity, Hygiene) and inline findings.
Contributors
- @dependabot – Authored the dependency bump commit for
github/codeql-action/upload-sarif. - @deepsource-autofix – Authored multiple “style: format code with Black and isort” commits.
- @deepsource-io – Performed DeepSource Code Review (static analysis on Python/JavaScript changes) with full review report and PR report card.
- @sourcery-ai – Generated the “Summary by Sourcery”, Reviewer’s Guide (including lifecycle flowchart), and detailed review comments with suggestions on process cleanup, fixture centralization, and canvas/title assertions.
- @coderabbitai – Generated the “Summary by CodeRabbit”, pre-merge checks, poem, and overall PR analysis covering CI/security, tests, and chores.
- @Copilot – Co-authored commits related to browser launch args handling and CDP/console listener cleanup.
👤 Human Contribution
- @ikostan – Primary author and driver of the PR. Implemented the core scene lifecycle and memory optimizations (shared Playwright page fixture, smarter Godot initialization, per-test JS heap metrics, robust CDP/V8 coverage teardown, threaded HTTP test server improvements, process tracking/cleanup, CI Chromium flags for reduced GPU/OOM risk, and refactoring of E2E tests to use shared helpers and deterministic waits). Linked issue #773, managed labels/milestone/project board, and iterated on the changes based on bot feedback.