{"record":{"id":"dda3276f5a79572c","repo":"reflex-dev/reflex","slug":"app-was-not-initialized","errorCode":null,"errorMessage":"App was not initialized.","messagePattern":"App was not initialized\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"reflex/testing.py","lineNumber":343,"sourceCode":"                    await self.app_instance.sio.shutdown()\n\n            # sqlalchemy async engine shutdown handler\n            if find_spec(\"sqlmodel\"):\n                try:\n                    async_engine = reflex.model.get_async_engine(None)\n                except ValueError:\n                    pass\n                else:\n                    await async_engine.dispose()\n\n            await original_shutdown(*args, **kwargs)\n\n        return _shutdown\n\n    def _start_backend(self, port: int = 0):\n        if self.app_asgi is None:\n            msg = \"App was not initialized.\"\n            raise RuntimeError(msg)\n        self.backend = uvicorn.Server(\n            uvicorn.Config(\n                app=self.app_asgi,\n                host=\"127.0.0.1\",\n                port=port,\n            )\n        )\n        self.backend.shutdown = self._get_backend_shutdown_handler()\n\n        def _run_backend(context: contextvars.Context) -> None:\n            if self.backend is not None:\n                context.run(self.backend.run)\n\n        with chdir(self.app_path):\n            print(  # noqa: T201\n                \"Creating backend in a new thread...\"\n            )  # for pytest diagnosis\n            self.backend_thread = threading.Thread(","sourceCodeStart":325,"sourceCodeEnd":361,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/reflex/testing.py#L325-L361","documentation":"RuntimeError raised by AppHarness._start_backend when self.app_asgi is None, meaning the ASGI app object was never imported/constructed. The harness needs the compiled Reflex app's ASGI interface before it can launch uvicorn.","triggerScenarios":"Calling harness._start_backend() (directly or via start()) before the app source was imported and app_asgi set — typically because app_source pointed to a module that never creates an `app = rx.App()` at module level, or the import failed silently earlier.","commonSituations":"Test app module that builds the App inside a function guarded by __main__; typo in the app_source import path so no app is found; refactor moved app creation out of module scope.","solutions":["Ensure the app_source module instantiates `app = rx.App()` (and add_page) at module level","Verify the app_source string/function actually exposes a Reflex App; fix the import path","Let AppHarness.create/start drive backend startup instead of calling _start_backend manually"],"exampleFix":"# before\n# myapp.py\nif __name__ == \"__main__\":\n    app = rx.App()\n\n# after\n# myapp.py\napp = rx.App()\napp.add_page(index)","handlingStrategy":"validation","validationCode":"import importlib\nmod = importlib.import_module(app_source_str)\nassert hasattr(mod, \"app\"), \"app_source module must expose `app = rx.App()` at module level\"","typeGuard":"from reflex.app import App\n\ndef module_has_app(module) -> bool:\n    return isinstance(getattr(module, \"app\", None), App)","tryCatchPattern":"try:\n    harness.start()\nexcept RuntimeError as e:\n    if \"App was not initialized\" in str(e):\n        raise RuntimeError(\"ensure app_source module defines app = rx.App()\") from e\n    raise","preventionTips":["Always create `app = rx.App()` at module level in test app modules","Smoke-test importing the app module before passing it to AppHarness"],"tags":["reflex","testing","app-harness","asgi","lifecycle"],"backgroundTag":"service-not-initialized","analyzedSha":"45b8ed5ab735f8a56bbb09a42384f030eb0208e7","analyzedAt":"2026-08-28T19:25:27.644Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}