Prhub

#30138 [fix] Wrap the sp_shard test entry point in sys.exit so failures propagate

原始 PR 作者 ch-wan 合并时间 2026-07-05 05:23 文件变更 1 提交数 1 评论 4 代码增减 +3 / -1

执行摘要

修复测试入口未传递退出码导致 CI 检查失败

在 PR #30107 中新增的 test_sp_shard.py__main__ 块中使用了裸的 pytest.main(...),这会导致退出码被吞掉。仓库中有一个 test_no_bare_pytest_main 的代码卫生检查会检测此类模式,该检查在 main 分支和每个开放 PR 的合并提交上都会失败,阻塞了 CI 流程。PR 说明中明确引用了断言失败信息:AssertionError: ['python/sglang/multimodal_gen/test/unit/test_sp_shard.py:213'] is not false : Found bare pytest.main(...) in __main__ blocks

该 PR 变更虽小但有必要,建议合并。它修复了因 #30107 引入的 CI 阻塞问题。值得注意的设计细节:仓库通过自动化测试 test_no_bare_pytest_main 强制执行代码规范(__main__ 中必须使用 sys.exit 包裹 pytest.main),这种做法有助于保持测试脚本质量,值得推广。

讨论亮点

无实质讨论。仅机器人自动评论确认变更正确,无人工 review 评论。PR 作者通过 /rerun-test 命令验证了修复:test_no_bare_pytest_main 测试通过,test_sp_shard.py 本身在 1-gpu-h100 上运行失败(但失败是预期行为,因为该测试需要 GPU 环境,而 CI 环境可能不支持)。

实现拆解

仅修改了一个文件 python/sglang/multimodal_gen/test/unit/test_sp_shard.py

  1. 在文件顶部增加了 import sys 导入。
  2. 将第 213 行的 pytest.main([__file__, "-q"]) 替换为 sys.exit(pytest.main([__file__, "-q"]))
    这样就可以将 pytest.main 的返回码传递给操作系统,当测试失败时脚本会以非零退出码退出,从而通过代码卫生检查。
文件 模块 状态 重要度
python/sglang/multimodal_gen/test/unit/test_sp_shard.py 测试 modified 3.99

关键源码片段

python/sglang/multimodal_gen/test/unit/test_sp_shard.py test-coverage

唯一被修改的文件,通过在 `__main__` 块中将 `pytest.main` 包裹在 `sys.exit` 中来修复退出码传递问题。

"""Unit tests for the unified SP shard helpers (pure logic, no distributed)."""
import sys # 新增:用于传递退出码import pytest
import torchfrom sglang.multimodal_gen.runtime.distributed import sp_shard_utils as sps
from sglang.multimodal_gen.runtime.distributed.sp_shard_utils import (
    SpShard,
    shard_like,
    tail_attn_meta,
)# ... (test functions omitted)if __name__ == "__main__":
    sys.exit(pytest.main([__file__, "-q"])) # 修改:用 sys.exit 包裹以确保退出码传递

评论区精华

没有提炼出高价值讨论线程

当前评论区没有形成足够清晰的争议点或结论,后续有更多讨论时会体现在这里。

风险与影响

风险极低。变更仅涉及测试文件入口点的退出码处理,不影响产品代码或测试逻辑本身。sys.exit 是标准做法,不会引入新的错误。

直接影响:修复了 CI 中的代码卫生检查,使其不再因 test_sp_shard.py 的入口点而失败,从而解除对 main 分支和其他 PR 的 CI 阻塞。间接影响:确保当直接运行该测试脚本时,测试失败能正确传递退出码,便于自动化脚本检测。

关联 Issue

未识别关联 Issue

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

完整报告

参与讨论