From b14d78cdc5efb92c4ca79aa551eb29e25a256b80 Mon Sep 17 00:00:00 2001 From: Benedikt Bartscher Date: Sat, 21 Nov 2026 22:42:49 +0100 Subject: [PATCH 1/2] perf: freeze the heap AppHarness started until it stops The harness compiles and runs the app in the test process, where everything the app keeps stays alive until stop(). Every full garbage collection walked all of it: for a generated app with 400 states and 400 pages, 915k objects and 350-430 ms per collection, stalling the backend thread's event loop past the dev-mode blocking detector's 0.2 s threshold. start() now collects and freezes the heap once the app runs, as production workers do, and stop() unfreezes it so the next app's collections can free this one. --- news/+harness-freeze-heap.performance.md | 1 + reflex/testing.py | 7 +++++ tests/units/test_testing.py | 34 ++++++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 news/+harness-freeze-heap.performance.md 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..c2b9c0701b2 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 @@ -537,6 +542,8 @@ def stop(self) -> None: self.backend_thread.join() if self.frontend_output_thread is not None: self.frontend_output_thread.join() + # Let the collector free this app now that it is gone. + gc.unfreeze() def __exit__(self, *excinfo) -> None: """Contextmanager protocol for `stop()`. diff --git a/tests/units/test_testing.py b/tests/units/test_testing.py index 82daa3541d1..02d87ebeb27 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,39 @@ class State(rx.State): assert harness.frontend_process.poll() is not None +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 = [] + + def start_step() -> None: + frozen_while_starting.append(gc.get_freeze_count()) + + for step in ( + "_initialize_app", + "_start_backend", + "_start_frontend", + "_wait_frontend", + ): + monkeypatch.setattr(harness, step, start_step) + 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 + + @pytest.fixture def harness_mocks(monkeypatch): """Common mocks for AppHarness initialization tests. From 2717e55d73d8a548c9ffd057f72804c54c97a4af Mon Sep 17 00:00:00 2001 From: Benedikt Bartscher Date: Sat, 21 Nov 2026 22:54:35 +0100 Subject: [PATCH 2/2] Unfreeze the heap first in AppHarness.stop() A cleanup step that raised skipped the unfreeze at the end of stop(), so the stopped app stayed frozen until another harness stopped cleanly. Unfreezing first covers every step. Shutdown runs after the test body, so a full collection during it only delays teardown. --- reflex/testing.py | 4 +-- tests/units/test_testing.py | 57 ++++++++++++++++++++++++++++++------- 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/reflex/testing.py b/reflex/testing.py index c2b9c0701b2..cabbbbaf616 100644 --- a/reflex/testing.py +++ b/reflex/testing.py @@ -497,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: @@ -542,8 +544,6 @@ def stop(self) -> None: self.backend_thread.join() if self.frontend_output_thread is not None: self.frontend_output_thread.join() - # Let the collector free this app now that it is gone. - gc.unfreeze() def __exit__(self, *excinfo) -> None: """Contextmanager protocol for `stop()`. diff --git a/tests/units/test_testing.py b/tests/units/test_testing.py index 02d87ebeb27..167636dfa4e 100644 --- a/tests/units/test_testing.py +++ b/tests/units/test_testing.py @@ -59,26 +59,37 @@ class State(rx.State): assert harness.frontend_process.poll() is not None -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. +def _stub_start_steps(monkeypatch, harness: AppHarness, step) -> None: + """Replace the steps of `AppHarness.start()` with one stand-in. Args: - tmp_path: pytest tmp_path fixture monkeypatch: pytest monkeypatch fixture + harness: The harness to stub. + step: The stand-in each step calls. """ - harness = AppHarness.create(root=tmp_path) - frozen_while_starting = [] - - def start_step() -> None: - frozen_while_starting.append(gc.get_freeze_count()) - - for step in ( + for name in ( "_initialize_app", "_start_backend", "_start_frontend", "_wait_frontend", ): - monkeypatch.setattr(harness, step, start_step) + 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: @@ -92,6 +103,30 @@ def start_step() -> None: 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.