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

- **ID:** `python/starlette-assertionerror-app-not-mounted`
- **领域:** python
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **Implement lifespan correctly** (90% 成功率)
   ```
   from contextlib import asynccontextmanager
@asynccontextmanager
async def lifespan(app):
    # startup
    yield
    # shutdown
app = Starlette(lifespan=lifespan)
   ```
2. **Use on_startup and on_shutdown directly** (85% 成功率)
   ```
   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** — Lifespan requires async generator or context manager. (80% 失败率)
- **Ignoring lifespan entirely** — Some servers require lifespan for startup/shutdown. (60% 失败率)
