python
runtime_error
ai_generated
true
test_foo 拆卸时出错 RuntimeError: 在 throw() 之后生成器没有停止
ERROR at teardown of test_foo RuntimeError: generator didn't stop after throw()
ID: python/pytest-session-fixture-finalizer-error
80%修复率
82%置信度
0证据数
2024-10-08首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
根因分析
基于 yield 的 fixture 在 yield 之后的 teardown 中引发异常,生成器未捕获,pytest 的 finalizer 机制报告生成器仍在运行。
English
A yield-based fixture raised an exception inside its teardown after the yield, and the generator did not catch it, so pytest's finalizer machinery reports the generator as still running.
解决方案
-
92% 成功率
@pytest.fixture def client(): c = connect() try: yield c finally: try: c.close() except Exception as e: log.warning('close failed: %s', e) -
90% 成功率
@pytest.fixture def client(request): c = connect() request.addfinalizer(c.close) return c
无效尝试
常见但无效的做法:
-
80% 失败
The error occurs during fixture teardown, outside the test body, so the test's try/except never sees it.
-
70% 失败
Loses teardown entirely; resources are never released and the error is replaced by a leak.