{"record":{"id":"f8083796c17b1e01","repo":"reflex-dev/reflex","slug":"backend-was-not-initialized","errorCode":null,"errorMessage":"Backend was not initialized.","messagePattern":"Backend was not initialized\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"reflex/testing.py","lineNumber":309,"sourceCode":"            # that were already imported so they can re-register memo definitions.\n            should_reload_app = (\n                self.app_source is not None or config.module in sys.modules\n            )\n            self.app_instance, self.app_module = (\n                reflex.utils.prerequisites.get_and_validate_app(\n                    reload=should_reload_app\n                )\n            )\n            self.app_asgi = self.app_instance()\n\n    def _reload_state_module(self):\n        \"\"\"Reload the rx.State module to avoid conflict when reloading.\"\"\"\n        reload_state_module(module=f\"{self.app_name}.{self.app_name}\")\n\n    def _get_backend_shutdown_handler(self):\n        if self.backend is None:\n            msg = \"Backend was not initialized.\"\n            raise RuntimeError(msg)\n\n        original_shutdown = self.backend.shutdown\n\n        async def _shutdown(*args, **kwargs) -> None:\n            # ensure redis is closed before event loop\n            if (\n                self.app_instance is not None\n                and self.app_instance._state_manager is not None\n            ):\n                with contextlib.suppress(ValueError):\n                    await self.app_instance._state_manager.close()\n\n            # socketio shutdown handler\n            if self.app_instance is not None and self.app_instance.sio is not None:\n                with contextlib.suppress(TypeError):\n                    await self.app_instance.sio.shutdown()\n\n            # sqlalchemy async engine shutdown handler","sourceCodeStart":291,"sourceCodeEnd":327,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/reflex/testing.py#L291-L327","documentation":"RuntimeError from AppHarness._get_backend_shutdown_handler when the harness's uvicorn backend instance is None — i.e. the backend server was never started. The shutdown handler wraps backend.shutdown to also close Redis, so a backend must exist first.","triggerScenarios":"Calling internal shutdown handling before AppHarness.start() completed, or when _start_backend failed/was skipped so self.backend was never assigned a uvicorn.Server.","commonSituations":"Manually invoking private harness methods in tests; a previous exception during start() leaving the harness half-initialized; reusing a harness after it was already shut down.","solutions":["Call `harness.start()` (or use `with AppHarness.create(...)` which starts automatically) and await/verify it succeeds before touching backend behavior","Check `harness.backend is not None` before calling backend-related internals","Don't call private methods like _get_backend_shutdown_handler directly; rely on the context-manager lifecycle"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"assert harness.backend is not None, \"call harness.start() before backend operations\"","typeGuard":"def backend_ready(h) -> bool:\n    return getattr(h, \"backend\", None) is not None","tryCatchPattern":"try:\n    harness.start()\nexcept RuntimeError as e:\n    if \"Backend was not initialized\" in str(e):\n        pytest.skip(\"backend failed to start\")\n    raise","preventionTips":["Always use AppHarness.create as a context manager","Never call private _* methods of AppHarness directly"],"tags":["reflex","testing","app-harness","lifecycle"],"backgroundTag":"service-not-initialized","analyzedSha":"45b8ed5ab735f8a56bbb09a42384f030eb0208e7","analyzedAt":"2026-08-28T19:25:27.644Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}