# pytest.xdist.WorkerInterrupted: 工作进程 'gw0' 在300秒后崩溃或超时

- **ID:** `python/pytest-xdist-worker-timeout`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

pytest-xdist中的测试工作进程执行时间超过指定的超时时间（默认300秒），通常是由于测试挂起、无限循环或外部资源缓慢。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.0.0 | active | — | — |
| 3.1.0 | active | — | — |

## 解决方案

1. **Set a longer timeout for specific tests using pytest.mark.timeout or xdist's --timeout option** (75% 成功率)
   ```
   pip install pytest-timeout
@pytest.mark.timeout(600)
def test_slow_operation():
    ...
Or run: pytest --timeout=600 -n auto
   ```
2. **Identify and fix the hanging test by running it in isolation with verbose logging** (90% 成功率)
   ```
   Run the test individually: pytest tests/test_slow.py -v --timeout=60
Add logging to identify where it hangs, then fix the infinite loop or resource issue.
   ```

## 无效尝试

- **Increasing the timeout limit globally without investigating the root cause** — This only delays the failure; the underlying issue (e.g., a hanging test) remains and may cause intermittent failures. (70% 失败率)
- **Disabling xdist entirely to avoid the timeout** — This removes parallel execution benefits, slowing down the test suite significantly. (40% 失败率)
