Prhub

#29407 Localize cur_batch field in Scheduler to avoid field-based state access

原始 PR 作者 fzyzcjy 合并时间 2026-07-10 08:55 文件变更 10 提交数 12 评论 1 代码增减 +60 / -48

执行摘要

调度器 cur_batch 字段局部化,仅看门狗使用

PR body 指出:self.cur_batch 是调度器范围属性,被每个事件循环写入,但唯一跨线程读者是看门狗,用作调试信号。所有循环内消费者应使用局部变量而非字段,以减少状态耦合。

值得精读,展示了逐步消除全局状态、局部化依赖的典型模式,适合调度器等核心模块重构参考。

讨论亮点

仅 gemini-code-assist[bot] 的一条评论指出 launch_batch_sample_if_needed 中新增的 cur_batch 参数可能为 None,建议类型标注为 Optional[ScheduleBatch] 并添加早期返回保护。该评论未获作者回应或修改,PR 已合并。

实现拆解

  1. 字段重命名:在 scheduler.py__init__init_running_status 中将 self.cur_batch 改为 self.cur_batch_for_debug
  2. 事件循环改造:在 event_loop_normalevent_loop_overlap、PP 事件循环、disaggregation 事件循环中,将 self.cur_batch = batch 改为 self.cur_batch_for_debug = batch,并保留局部变量 cur_batch 用于后续消费。
  3. 函数参数化launch_batch_sample_if_needed 增加 cur_batch: ScheduleBatch 参数,调用方传入局部变量;_pp_launch_batch 签名增加 cur_batch 参数。
  4. 看门狗适配:在 invariant_checker.pycreate_scheduler_watchdog 中,将 is_activedump_info 中的 scheduler.cur_batch 改为 scheduler.cur_batch_for_debug
  5. MLX 与 disaggregation 同步:更新 mlx/scheduler_mixin.pydisaggregation/decode.pydisaggregation/prefill.py 中的对应赋值。
  6. 测试适配:修改四个测试文件中对 cur_batch 的直接引用,改为 cur_batch_for_debug
文件 模块 状态 重要度
python/sglang/srt/managers/scheduler_pp_mixin.py 调度器 modified 6.87
python/sglang/srt/managers/scheduler.py 调度器 modified 6.27
python/sglang/srt/managers/scheduler_components/invariant_checker.py 调度器 modified 5.8
python/sglang/srt/hardware_backend/mlx/scheduler_mixin.py 调度器 modified 5.46
python/sglang/srt/disaggregation/decode.py 调度器 modified 5.31
python/sglang/srt/disaggregation/prefill.py 调度器 modified 5.31
test/registered/unit/managers/test_scheduler_pause_generation.py 测试 modified 4.98
test/registered/chunked_prefill/test_scripted_core_4gpu.py 测试 modified 4.17
test/registered/unit/disaggregation/test_decode_queue_cleanup.py 测试 modified 3.7
test/registered/unit/hardware_backend/mlx/test_attention_patching.py 测试 modified 3.7

关键符号

launch_batch_sample_if_needed _pp_launch_batch

关键源码片段

python/sglang/srt/managers/scheduler_pp_mixin.py core-logic

PP 主循环的核心改造:引入局部 cur_batch,镜像到 cur_batch_for_debug,并传参给 _pp_launch_batch。

def event_loop_pp(self: Scheduler):
    """PP 主循环,使用局部 cur_batch 而非 self.cur_batch。"""
    self.init_pp_loop_state()
    while True:
        server_is_idle = True
        for mb_id in range(self.pp_loop_size):
            self.running_batch = self.running_mbs[mb_id]
            self.last_batch = self.last_mbs[mb_id]
            # 获取下一个 batch
            self.mbs[mb_id] = self.get_next_batch_to_run()
            self.running_mbs[mb_id] = self.running_batch
            cur_batch: Optional[ScheduleBatch] = self.mbs[mb_id]
            # 仅看门狗使用 cur_batch_for_debug
            self.cur_batch_for_debug = cur_batch
            if cur_batch:
                server_is_idle = False
                pp_proxy_tensors = self._pp_recv_proxy_tensors()
            # ... 其他处理 ...
            if cur_batch:
                # 显式传入局部变量,不依赖字段
                result, self.launch_event = self._pp_launch_batch(
                    mb_id, cur_batch, pp_proxy_tensors,
                    self.mb_metadata, self.last_rank_comm_queue,
                )
            # ... 继续 ...

评论区精华

launch_batch_sample_if_needed 参数可能为 None 正确性

gemini-code-assist[bot] 指出新增的 cur_batch 参数可能为 None(例如事件循环无 batch 时),调用时会导致 AttributeError,建议标为 Optional 并加早期返回。

结论:作者未回应或修改,PR 已合并,认为实际调用路径不会传入 None。 · 待处理

风险与影响

风险较低:纯重命名+参数化,无行为变更。但若有外部代码直接引用 self.cur_batch 会引发 AttributeError。通过等价性审计和测试覆盖确保无遗漏。

对用户无功能影响;对开发者,字段重命名需适应,但提升了调度器内部状态管理清晰度。团队需更新依赖 self.cur_batch 的私有插件或分支。

字段重命名可能遗漏引用 部分调用可能传入 None

关联 Issue

未识别关联 Issue

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

完整报告

参与讨论