python
runtime_error
ai_generated
true
RuntimeError: 在 fixture 'database' 的 teardown 过程中出错 (得到异常: OperationalError: database is locked)
RuntimeError: Error during teardown of fixture 'database' (got exception: OperationalError: database is locked)
ID: python/pytest-fixture-finalizer-error
80%修复率
85%置信度
0证据数
2024-12-01首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.8 | active | — | — | — |
| 3.9 | active | — | — | — |
| 3.10 | active | — | — | — |
| 3.11 | active | — | — | — |
| 3.12 | active | — | — | — |
根因分析
fixture 的 finalizer(teardown)抛出异常,通常是因为在尝试关闭数据库连接时,连接仍在使用中或被锁定。
English
The fixture's finalizer (teardown) raises an exception, often because the database connection is still in use or locked when trying to close it.
解决方案
-
90% 成功率
Ensure all database transactions are committed or rolled back before teardown: `yield db; db.close()`
-
95% 成功率
Use a context manager for the database connection to guarantee proper cleanup.
无效尝试
常见但无效的做法:
-
70% 失败
This is unreliable; the lock might persist longer, and sleeping wastes time.
-
85% 失败
This hides potential resource leaks and can cause issues in subsequent tests.