python
runtime_error
ai_generated
true
httpx.ConnectError: 所有连接尝试均失败(lifespan 启动从未执行)
httpx.ConnectError: All connection attempts failed (lifespan startup never executed)
ID: python/starlette-testclient-lifespan-not-run
80%修复率
86%置信度
0证据数
2024-04-02首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 0.100+ | active | — | — | — |
| 0.110+ | active | — | — | — |
根因分析
在未运行 lifespan 的情况下使用 httpx.Client 访问应用,导致 app.state 资源(数据库池、客户端)为 None。
English
Using httpx.Client against app without running lifespan, so app.state resources (DB pool, clients) are None.
解决方案
-
95% 成功率
from starlette.testclient import TestClient with TestClient(app) as client: # 'with' runs lifespan r = client.get('/health') -
88% 成功率
Use httpx.ASGITransport with lifespan='on' via asgi-lifespan LifespanManager.
无效尝试
常见但无效的做法:
-
65% 失败
Misses teardown; leaks resources and diverges from prod.
-
85% 失败
Still no lifespan; same None state.