# RuntimeError: You cannot use the ASGI lifespan protocol with a sync application

- **ID:** `python/starlette-runtime-error-application-context`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Starlette 应用被配置为异步生命周期，但应用本身是同步函数

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.10 | active | — | — |
| 3.11 | active | — | — |

## Workarounds

1. **将应用改为异步函数或使用同步生命周期** (90% success)
   ```
   app = Starlette(routes=routes)  # 使用同步应用
或者
async def app(scope, receive, send): ...
   ```
2. **使用 starlette.applications.Starlette 的 lifespan 参数** (95% success)
   ```
   async def lifespan(app):
    yield
app = Starlette(routes=routes, lifespan=lifespan)
   ```

## Dead Ends

- **移除 lifespan 参数但保留 async def 应用** — 应用仍为异步，但缺少生命周期管理，可能导致资源泄漏 (50% fail)
- **使用 sync_to_async 包装应用但未正确配置** — 包装后仍需要异步上下文，根本问题未解决 (75% fail)
