# OSError: [Errno 28] 设备上没有剩余空间: '/tmp/pytest-of-runner/pytest-123/test_x0/data.bin'

- **ID:** `python/pytest-tmp-path-not-cleaned`
- **领域:** python
- **类别:** system_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

测试向 tmp_path 写入大文件，加上 pytest 的 tmp_path 保留策略（默认保留 3 次运行）和并行 xdist worker，数据累积速度快于清理速度。

## 版本兼容性

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

## 解决方案

1. **** (90% 成功率)
   ```
   # pyproject.toml
[tool.pytest.ini_options]
# pytest keeps last 3 by default; use a fixture to clean large files
def test_big(tmp_path):
    p = tmp_path / 'data.bin'
    try:
        p.write_bytes(b'x' * 10_000_000)
        ...
    finally:
        p.unlink(missing_ok=True)
   ```
2. **** (88% 成功率)
   ```
   # CI step
pytest --basetemp=/tmp/pytest-$CI_JOB_ID tests/
rm -rf /tmp/pytest-$CI_JOB_ID

# Or in conftest:
import shutil, pytest
@pytest.fixture(scope='session', autouse=True)
def _clean_basetemp(request):
    yield
    shutil.rmtree(request.config.option.basetemp, ignore_errors=True)
   ```

## 无效尝试

- **** — Buying time, not fixing the leak; suites that grow will hit the same limit and the cost increases. (80% 失败率)
- **** — RAM disk is smaller than /tmp; large test artifacts fill it faster and cause OOM or ENOSPC sooner. (85% 失败率)
