python config_error ai_generated true

AssertionError: ASGI 应用未挂载到生命周期

AssertionError: ASGI app not mounted on lifespan

ID: python/starlette-assertionerror-app-not-mounted

其他格式: JSON · Markdown 中文 · English
80%修复率
80%置信度
0证据数
2024-07-28首次发现

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

Starlette 应用程序中的生命周期处理程序未正确配置。

English

Lifespan handler is not properly configured in the Starlette application.

generic

解决方案

  1. 90% 成功率 Implement lifespan correctly
    from contextlib import asynccontextmanager
    @asynccontextmanager
    async def lifespan(app):
        # startup
        yield
        # shutdown
    app = Starlette(lifespan=lifespan)
  2. 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'))

无效尝试

常见但无效的做法:

  1. Adding lifespan without async context manager 80% 失败

    Lifespan requires async generator or context manager.

  2. Ignoring lifespan entirely 60% 失败

    Some servers require lifespan for startup/shutdown.