python
config_error
ai_generated
true
AssertionError: ASGI 应用未挂载到生命周期
AssertionError: ASGI app not mounted on lifespan
ID: python/starlette-assertionerror-app-not-mounted
80%修复率
80%置信度
0证据数
2024-07-28首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
Starlette 应用程序中的生命周期处理程序未正确配置。
English
Lifespan handler is not properly configured in the Starlette application.
解决方案
-
90% 成功率 Implement lifespan correctly
from contextlib import asynccontextmanager @asynccontextmanager async def lifespan(app): # startup yield # shutdown app = Starlette(lifespan=lifespan) -
85% 成功率 Use on_startup and on_shutdown directly
async def startup(): print('start') app.add_event_handler('startup', startup) app.add_event_handler('shutdown', lambda: print('stop'))
无效尝试
常见但无效的做法:
-
Adding lifespan without async context manager
80% 失败
Lifespan requires async generator or context manager.
-
Ignoring lifespan entirely
60% 失败
Some servers require lifespan for startup/shutdown.