# test_x 的 teardown 出错
RuntimeError: generator didn't stop after throw()
# 或: ERROR: test_x - Exception: teardown error swallowed

- **ID:** `python/pytest-fixture-finalizer-exception-masked`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

yield fixture 的 teardown 抛出异常，掩盖了原始测试失败，或生成器在 yield 后被重新进入，导致 pytest 报告 teardown 错误。

## 版本兼容性

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

## 解决方案

1. **** (90% 成功率)
   ```
   @pytest.fixture
def resource(request):
    r = acquire()
    request.addfinalizer(r.close)
    return r
   ```
2. **** (85% 成功率)
   ```
   @pytest.fixture
def resource():
    r = acquire()
    yield r
    try:
        r.close()
    except Exception:
        import logging
        logging.exception('teardown failed')
        raise
   ```

## 无效尝试

- **** — Swallows real cleanup errors; resources may remain locked and subsequent tests fail. (75% 失败率)
- **** — Delays teardown to the end of the run but does not prevent the exception from masking failures. (60% 失败率)
