Prhub

#26714 fix test cases failed in nightly pipeline

原始 PR 作者 liuxianglong17 合并时间 2026-06-01 11:33 文件变更 2 提交数 2 评论 5 代码增减 +7 / -0

执行摘要

修复 NPU 夜间测试因超时失败

PR body 指出:test_ascend_minimax_m2.py 因服务启动时间超过断言时间而失败;test_npu_original_logprobs.pytest_npu_bge_reranker_v2_m3.py 因清理进程超时失败。作者通过增加等待时间和预处理步骤来规避失败。

值得快速合入,属于典型的 CI 稳定性修复。关注其中 RPC 套接字关闭模式(linger=0 + getattr 保护),可作为其他 shutdown 场景的参考模式。

讨论亮点

Review 中 gemini-code-assist[bot] 提出两个问题:

  • AttributeError 风险:若 _launch_subprocesses__init__ 中失败,atexit 调用 shutdown() 时直接访问 self.send_to_rpc 可能引发 AttributeError,建议使用 getattr。作者采纳该建议,在最终代码中使用 getattr(self, "send_to_rpc", None)
  • ZMQ 关闭可能永久阻塞:建议设置 linger=0 避免 hang。作者采纳,最终代码使用 send_to_rpc.close(linger=0)
  • 服务启动超时值:bot 建议 600 秒,作者实测 600 秒仅加载 40%,改为 1800 秒并说明理由。

实现拆解

  1. RPC 套接字优雅关闭:在 python/sglang/srt/entrypoints/engine.pyshutdown() 方法中,在调用 kill_process_tree 之前,先获取并关闭 send_to_rpc ZMQ 套接字(设置 linger=0 避免阻塞),防止残留套接字句柄导致进程树杀死超时。
  2. MiniMaxM2 测试启动超时提升:在 test/registered/ascend/llm_models/test_ascend_minimax_m2.pyTestMiniMaxM2 类中增加类属性 timeout_for_server_launch = 1800,将服务启动等待时间从默认值延长至 30 分钟,适配 NPU 环境较慢的模型加载。
文件 模块 状态 重要度
python/sglang/srt/entrypoints/engine.py 引擎入口 modified 5.96
test/registered/ascend/llm_models/test_ascend_minimax_m2.py NPU 测试 modified 3.49

关键符号

shutdown

关键源码片段

python/sglang/srt/entrypoints/engine.py core-logic

引擎入口 shutdown 逻辑,增加 RPC 套接字优雅关闭,直接解决清理进程超时问题。

def shutdown(self):
    """Shutdown the engine; block until the scheduler subprocess releases
    its GPU context so the caller can immediately reallocate on the same device."""
    if (
        self.tokenizer_manager is not None
        and self.tokenizer_manager._subprocess_watchdog is not None
    ):
        self.tokenizer_manager._subprocess_watchdog.stop()
​
    # 使用 getattr 避免 __init__ 未完成时 send_to_rpc 不存在导致 AttributeError
    send_to_rpc = getattr(self, "send_to_rpc", None)
    if send_to_rpc is not None:
        # linger=0 确保立即关闭,避免 shutdown hang
        send_to_rpc.close(linger=0)
        self.send_to_rpc = None
​
    kill_process_tree(os.getpid(), include_parent=False, wait_timeout=60)

评论区精华

AttributeError 风险与 ZMQ 关闭 linger 设置 正确性

gemini-code-assist[bot] 指出直接访问 self.send_to_rpc 可能因初始化失败而引发 AttributeError,且 ZMQ close() 可能永久阻塞,建议使用 getattr 和 linger=0。

结论:作者采纳建议,最终代码使用 getattr 和 linger=0。 · 已解决

服务启动超时值的选择 测试

bot 建议 timeout_for_server_launch 设为 600 秒,作者实测 600 秒仅加载 40%,改为 1800 秒并解释原因。

结论:最终采用 1800 秒,作者说明理由。 · 已解决

风险与影响

风险较低。shutdown() 中的 RPC 关闭逻辑仅在进程退出时执行,不会影响正常运行路径;增加的超时值仅对单个测试类生效,不会影响其他测试。但需注意,若 NPU 环境进一步变慢,1800 秒仍可能不够,届时需再次调整。

影响范围限于 NPU 夜间测试流水线中的三个特定测试用例。对系统核心功能无影响,对用户无感知。对团队而言,减少了 nightly CI 的不稳定失败,节省排查时间。

NPU 环境依赖 超时值可能需再次调整

关联 Issue

未识别关联 Issue

当前没有检测到明确关联的 Issue 链接,后续同步到相关引用后会出现在这里。

完整报告

参与讨论