From a297cd3da1322593e0f02e0cadf3bb94ae4740c9 Mon Sep 17 00:00:00 2001 From: Yufeng He <40085740+he-yufeng@users.noreply.github.com> Date: Mon, 12 Oct 2026 12:56:19 +0800 Subject: [PATCH] [Fix] scheduler: stop latching batch_is_full when nothing can run (#41872) Co-authored-by: hnyls2002 Co-authored-by: Liangsheng Yin --- python/sglang/srt/managers/scheduler.py | 19 +++---- .../test_scheduler_batch_is_full_latch.py | 51 +++++++++++++++++++ 2 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 test/registered/unit/managers/test_scheduler_batch_is_full_latch.py diff --git a/python/sglang/srt/managers/scheduler.py b/python/sglang/srt/managers/scheduler.py index 5e548aef47..0a7bc46725 100644 --- a/python/sglang/srt/managers/scheduler.py +++ b/python/sglang/srt/managers/scheduler.py @@ -3942,6 +3942,10 @@ class Scheduler( if self.enable_priority_preemption or self.is_hybrid_swa: # Reset batch_is_full to try preemption with a prefill adder. running_batch.batch_is_full = False + if running_batch.is_empty(): + # An empty batch never reaches update_running_batch, the only + # other place that clears the flag, so admission would stay shut. + running_batch.batch_is_full = False if ( running_batch.batch_is_full or len(self.waiting_queue) == 0 @@ -4127,17 +4131,10 @@ class Scheduler( if res != AddReqResult.CONTINUE: if res == AddReqResult.NO_TOKEN: - if ( - self.enable_hierarchical_cache - or self.enable_lmcache - or self.enable_unified_cache_external_linker - ): - # Set batch_is_full after making sure there are requests that can be served - running_batch.batch_is_full = len(adder.can_run_list) > 0 or ( - not running_batch.is_empty() - ) - else: - running_batch.batch_is_full = True + # Set batch_is_full after making sure there are requests that can be served + running_batch.batch_is_full = len(adder.can_run_list) > 0 or ( + not running_batch.is_empty() + ) # revert matched mamba idx to avoid memory leak, if req is not added. # Only free if the slot was freshly allocated in this batch (not # pre-existing from a session). Session-held slots have their own diff --git a/test/registered/unit/managers/test_scheduler_batch_is_full_latch.py b/test/registered/unit/managers/test_scheduler_batch_is_full_latch.py new file mode 100644 index 0000000000..66ecab75d3 --- /dev/null +++ b/test/registered/unit/managers/test_scheduler_batch_is_full_latch.py @@ -0,0 +1,51 @@ +"""An empty running batch never keeps admission closed (#41841).""" + +import unittest +from types import SimpleNamespace +from unittest.mock import Mock + +from sglang.test.ci.ci_register import register_cpu_ci +from sglang.test.test_utils import CustomTestCase, maybe_stub_sgl_kernel + +maybe_stub_sgl_kernel() + +from sglang.srt.managers.schedule_batch import ScheduleBatch +from sglang.srt.managers.scheduler import Scheduler + +register_cpu_ci(est_time=2, suite="base-a-test-cpu") + + +class _AdmissionAttempted(Exception): + pass + + +class TestEmptyBatchFullLatch(CustomTestCase): + def _scheduler(self) -> Scheduler: + s = Scheduler.__new__(Scheduler) + s.grammar_manager = SimpleNamespace(has_waiting_grammars=lambda: False) + s.enable_priority_preemption = False + s.is_hybrid_swa = False + s.waiting_queue = [SimpleNamespace(rid="req-0")] + s.chunked_req = None + s.min_free_slots_delayer = None + s.get_num_allocatable_reqs = Mock(return_value=8) + s.processed_tokens_counter = 0 + # Reaching the policy means the admission gates were passed. + s.policy = SimpleNamespace(calc_priority=Mock(side_effect=_AdmissionAttempted)) + return s + + def test_latched_empty_batch_still_admits(self): + # Whichever pass set the flag on an empty batch (no KV room, or no free + # request slot), no later step shrinks the batch to clear it. + s = self._scheduler() + running_batch = ScheduleBatch(reqs=[], batch_is_full=True) + + with self.assertRaises(_AdmissionAttempted): + s._get_new_batch_prefill_raw( + prefill_delayer_single_pass=None, running_batch=running_batch + ) + self.assertFalse(running_batch.batch_is_full) + + +if __name__ == "__main__": + unittest.main()