# PR #26714 完整报告

- 仓库：`sgl-project/sglang`
- 标题：fix test cases failed in nightly pipeline
- 合并时间：2026-06-01 11:33
- 原文链接：http://prhub.com.cn/sgl-project/sglang/pull/26714

---

# 执行摘要

- 一句话：修复 NPU 夜间测试因超时失败
- 推荐动作：值得快速合入，属于典型的 CI 稳定性修复。关注其中 RPC 套接字关闭模式（`linger=0` + `getattr` 保护），可作为其他 `shutdown` 场景的参考模式。

# 功能与动机

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

# 实现拆解

1. **RPC 套接字优雅关闭**：在 `python/sglang/srt/entrypoints/engine.py` 的 `shutdown()` 方法中，在调用 `kill_process_tree` 之前，先获取并关闭 `send_to_rpc` ZMQ 套接字（设置 `linger=0` 避免阻塞），防止残留套接字句柄导致进程树杀死超时。
2. **MiniMaxM2 测试启动超时提升**：在 `test/registered/ascend/llm_models/test_ascend_minimax_m2.py` 的 `TestMiniMaxM2` 类中增加类属性 `timeout_for_server_launch = 1800`，将服务启动等待时间从默认值延长至 30 分钟，适配 NPU 环境较慢的模型加载。

关键文件：
- `python/sglang/srt/entrypoints/engine.py`（模块 引擎入口；类别 source；类型 core-logic；符号 shutdown）: 引擎入口 shutdown 逻辑，增加 RPC 套接字优雅关闭，直接解决清理进程超时问题。
- `test/registered/ascend/llm_models/test_ascend_minimax_m2.py`（模块 NPU 测试；类别 test；类型 test-coverage；符号 TestMiniMaxM2）: 测试用例，增加服务启动超时时间，避免因模型加载慢导致失败。

关键符号：shutdown

## 关键源码片段

### `python/sglang/srt/entrypoints/engine.py`

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

```python
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)

```

# 评论区精华

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 秒并说明理由。

 - AttributeError 风险与 ZMQ 关闭 linger 设置 (correctness): 作者采纳建议，最终代码使用 getattr 和 linger=0。
- 服务启动超时值的选择 (testing): 最终采用 1800 秒，作者说明理由。

# 风险与影响

- 风险：风险较低。`shutdown()` 中的 RPC 关闭逻辑仅在进程退出时执行，不会影响正常运行路径；增加的超时值仅对单个测试类生效，不会影响其他测试。但需注意，若 NPU 环境进一步变慢，1800 秒仍可能不够，届时需再次调整。
- 影响：影响范围限于 NPU 夜间测试流水线中的三个特定测试用例。对系统核心功能无影响，对用户无感知。对团队而言，减少了 nightly CI 的不稳定失败，节省排查时间。
- 风险标记：NPU 环境依赖 , 超时值可能需再次调整

# 关联脉络

- 暂无明显关联 PR