diff --git a/news/+harness-freeze-heap.performance.md b/news/+harness-freeze-heap.performance.md new file mode 100644 index 00000000000..25ee5e1c407 --- /dev/null +++ b/news/+harness-freeze-heap.performance.md @@ -0,0 +1 @@ +`AppHarness` freezes the objects the started app built, so a full garbage collection during an integration test no longer walks the whole compiled app and stalls the backend for seconds. diff --git a/reflex/testing.py b/reflex/testing.py index 1a68919ab63..cabbbbaf616 100644 --- a/reflex/testing.py +++ b/reflex/testing.py @@ -7,6 +7,7 @@ import contextvars import dataclasses import functools +import gc import inspect import logging import os @@ -463,6 +464,10 @@ def start(self) -> Self: self._start_backend() self._start_frontend() self._wait_frontend() + # The app lives until stop(): full collections skip it instead of walking + # it, which stalls the backend's event loop in a large app. + gc.collect() + gc.freeze() return self @staticmethod @@ -492,6 +497,8 @@ def __enter__(self) -> Self: def stop(self) -> None: """Stop the frontend and backend servers.""" + # First, so the collector can free this app even if a step below raises. + gc.unfreeze() try: import psutil except ImportError as exc: diff --git a/tests/units/test_testing.py b/tests/units/test_testing.py index 82daa3541d1..167636dfa4e 100644 --- a/tests/units/test_testing.py +++ b/tests/units/test_testing.py @@ -1,5 +1,6 @@ """Unit tests for the included testing tools.""" +import gc import sys from types import ModuleType, SimpleNamespace from unittest import mock @@ -58,6 +59,74 @@ class State(rx.State): assert harness.frontend_process.poll() is not None +def _stub_start_steps(monkeypatch, harness: AppHarness, step) -> None: + """Replace the steps of `AppHarness.start()` with one stand-in. + + Args: + monkeypatch: pytest monkeypatch fixture + harness: The harness to stub. + step: The stand-in each step calls. + """ + for name in ( + "_initialize_app", + "_start_backend", + "_start_frontend", + "_wait_frontend", + ): + monkeypatch.setattr(harness, name, step) + + +def test_app_harness_freezes_the_started_app_until_stopped(tmp_path, monkeypatch): + """The heap is frozen once the app has started, and unfrozen when it stops. + + Args: + tmp_path: pytest tmp_path fixture + monkeypatch: pytest monkeypatch fixture + """ + harness = AppHarness.create(root=tmp_path) + frozen_while_starting = [] + _stub_start_steps( + monkeypatch, + harness, + lambda: frozen_while_starting.append(gc.get_freeze_count()), + ) + monkeypatch.setattr(harness, "_reload_state_module", lambda: None) + + try: + harness.start() + frozen_while_running = gc.get_freeze_count() + finally: + harness.stop() + + assert frozen_while_starting == [0, 0, 0, 0] + assert frozen_while_running > 0 + assert gc.get_freeze_count() == 0 + + +def test_app_harness_unfreezes_when_a_stop_step_raises(tmp_path, monkeypatch): + """A failing cleanup step does not leave the heap frozen. + + Args: + tmp_path: pytest tmp_path fixture + monkeypatch: pytest monkeypatch fixture + """ + harness = AppHarness.create(root=tmp_path) + _stub_start_steps(monkeypatch, harness, lambda: None) + + def failing_cleanup() -> None: + msg = "cleanup failed" + raise RuntimeError(msg) + + monkeypatch.setattr(harness, "_reload_state_module", failing_cleanup) + harness.start() + + with pytest.raises(RuntimeError) as exc_info: + harness.stop() + + assert exc_info.value.args == ("cleanup failed",) + assert gc.get_freeze_count() == 0 + + @pytest.fixture def harness_mocks(monkeypatch): """Common mocks for AppHarness initialization tests.