fix(app): pass Reflex app instance to lifespan task app parameter#6358
fix(app): pass Reflex app instance to lifespan task app parameter#6358BABTUNA wants to merge 1 commit intoreflex-dev:mainfrom
Conversation
Greptile SummaryThis PR fixes lifespan task parameter injection so that tasks declaring an Confidence Score: 5/5Safe to merge — the fix is correct and non-breaking; only remaining feedback is a P2 test coverage suggestion. The core logic change is a one-line substitution ( No files require special attention. Important Files Changed
Sequence DiagramsequenceDiagram
participant SA as Starlette (ASGI)
participant LM as LifespanMixin._run_lifespan_tasks(starlette_app)
participant T as Lifespan Task
SA->>LM: call with starlette_app instance
LM->>LM: inspect.signature(task)
alt task has "app" param
LM->>LM: partial(task, app=self) — Reflex instance
end
alt task has "starlette_app" param
LM->>LM: partial(task, starlette_app=starlette_app) — Starlette instance
end
LM->>T: task()
T-->>LM: coroutine / context manager / plain return
Reviews (1): Last reviewed commit: "Pass Reflex app instance to lifespan tas..." | Re-trigger Greptile |
| @pytest.mark.asyncio | ||
| async def test_lifespan_task_app_param_receives_reflex_app_instance(): | ||
| """Lifespan tasks should receive the Reflex app instance, not Starlette.""" | ||
|
|
||
| class DummyApp(LifespanMixin): | ||
| """Minimal test app based on the lifespan mixin.""" | ||
|
|
||
| app = DummyApp() | ||
| received: dict[str, object] = {} | ||
|
|
||
| def lifespan_task(app): | ||
| """Record the app argument injected by the lifespan runner. | ||
|
|
||
| Args: | ||
| app: App object injected by the lifespan runner. | ||
| """ | ||
| received["app"] = app | ||
|
|
||
| app.register_lifespan_task(lifespan_task) | ||
|
|
||
| async with app._run_lifespan_tasks(Starlette()): | ||
| await asyncio.sleep(0) | ||
|
|
||
| assert received["app"] is app |
There was a problem hiding this comment.
Missing test coverage for
starlette_app injection
The PR introduces starlette_app as a new injectable parameter for lifespan tasks, but the test suite only verifies the app (Reflex instance) injection path. A complementary test covering starlette_app injection — and ideally a task that declares both parameters simultaneously — would complete coverage for the new feature.
All Submissions:
github.com/reflex-dev/reflex/blob/main/CONTRIBUTING.md) file?
Requests for the desired
changed?
Type of change
New Feature Submission:
Changes To Core Features:
like us to include them?
Description
This fixes lifespan task app injection so tasks that declare an
appparameter receive the Reflex app instance (
rx.App) instead of theStarlette wrapper.
Before:
_run_lifespan_tasks, tasks with anappparameter were called withthe Starlette lifespan app object.
appinconsistent with expected Reflex app behavior.After:
appparameter now receivesself(the Reflex app instance).starlette_appparameter support for tasks thatexplicitly want the Starlette instance.
Tests
Added regression test:
tests/units/app_mixins/test_lifespan.pytest_lifespan_task_app_param_receives_reflex_app_instanceValidation run:
uv run pytest tests/units/app_mixins/test_lifespan.py -quv run ruff check reflex/app_mixins/lifespan.py tests/units/app_mixins/ test_lifespan.pyCloses #6331