python runtime_error ai_generated true

Failed: 来自 pytest-timeout 的超时(>30.0s)。 E Failed: 来自 pytest-timeout 的超时(>30.0s)。

Failed: Timeout (>30.0s) from pytest-timeout. E Failed: Timeout (>30.0s) from pytest-timeout.

ID: python/pytest-timeout-fixture-hang

其他格式: JSON · Markdown 中文 · English
80%修复率
84%置信度
0证据数
2024-11-19首次发现

版本兼容性

版本状态引入弃用备注
2.x active — — —

根因分析

fixture 打开资源(socket、子进程、锁)并无限阻塞;pytest-timeout 中止测试但 fixture 的 teardown 从未运行。

English

A fixture opens a resource (socket, subprocess, lock) and blocks indefinitely; pytest-timeout aborts the test but the fixture teardown never runs.

generic

解决方案

  1. 90% 成功率
    import socket
    
    @pytest.fixture
    def server():
        s = socket.socket()
        s.settimeout(5)
        try:
            s.connect(('localhost', 8080))
        except socket.timeout:
            pytest.skip('server not reachable')
        yield s
        s.close()
  2. 85% 成功率
    pytest --timeout=30 --timeout-method=thread --timeout-func-only -q
    # Or set in pytest.ini:
    # [pytest]
    # timeout = 30
    # timeout_method = thread

无效尝试

常见但无效的做法:

  1. 70% 失败

    The hang is real, not slow; increasing the timeout only delays the failure and wastes CI time.

  2. 90% 失败

    Removes the safety net; CI hangs forever.