From 9edb0e0da341a18707b4422957cc334b12116aac Mon Sep 17 00:00:00 2001 From: Zilin Zhu Date: Sun, 16 Nov 2025 22:02:22 +0800 Subject: [PATCH] Add SGLANG_ENABLE_REQ_POOL_LEAK_STRICT_CHECK to bypass mem leak check (#13339) --- python/sglang/srt/environ.py | 1 + .../scheduler_runtime_checker_mixin.py | 21 ++++++++++++++++--- python/sglang/srt/utils/common.py | 10 +++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/python/sglang/srt/environ.py b/python/sglang/srt/environ.py index 86936bccb..ee5b42349 100644 --- a/python/sglang/srt/environ.py +++ b/python/sglang/srt/environ.py @@ -136,6 +136,7 @@ class Envs: SGLANG_LOG_GC = EnvBool(False) SGLANG_LOG_FORWARD_ITERS = EnvBool(False) SGLANG_DISABLE_REQUEST_LOGGING = EnvBool(False) + SGLANG_ENABLE_STRICT_MEM_CHECK_DURING_IDLE = EnvBool(True) # Test & Debug SGLANG_IS_IN_CI = EnvBool(False) diff --git a/python/sglang/srt/managers/scheduler_runtime_checker_mixin.py b/python/sglang/srt/managers/scheduler_runtime_checker_mixin.py index e6f59e5b0..5870a13cd 100644 --- a/python/sglang/srt/managers/scheduler_runtime_checker_mixin.py +++ b/python/sglang/srt/managers/scheduler_runtime_checker_mixin.py @@ -7,10 +7,15 @@ import time from typing import TYPE_CHECKING from sglang.srt.disaggregation.utils import DisaggregationMode +from sglang.srt.environ import envs from sglang.srt.managers.schedule_batch import ScheduleBatch from sglang.srt.mem_cache.mamba_radix_cache import MambaRadixCache from sglang.srt.mem_cache.swa_radix_cache import SWARadixCache -from sglang.srt.utils.common import disable_request_logging, pyspy_dump_schedulers +from sglang.srt.utils.common import ( + disable_request_logging, + pyspy_dump_schedulers, + raise_error_or_warn, +) if TYPE_CHECKING: from sglang.srt.managers.scheduler import Scheduler @@ -138,7 +143,12 @@ class SchedulerRuntimeCheckerMixin: f"available_size={len(self.req_to_token_pool.free_slots)}, " f"total_size={self.req_to_token_pool.size}\n" ) - raise ValueError(msg) + raise_error_or_warn( + self, + envs.SGLANG_ENABLE_STRICT_MEM_CHECK_DURING_IDLE, + "count_req_pool_leak_warnings", + msg, + ) def check_memory(self: Scheduler): if self.is_hybrid: @@ -150,7 +160,12 @@ class SchedulerRuntimeCheckerMixin: if memory_leak: msg = "token_to_kv_pool_allocator memory leak detected! " f"{token_msg}" - raise ValueError(msg) + raise_error_or_warn( + self, + envs.SGLANG_ENABLE_STRICT_MEM_CHECK_DURING_IDLE, + "count_memory_leak_warnings", + msg, + ) self._check_req_pool() diff --git a/python/sglang/srt/utils/common.py b/python/sglang/srt/utils/common.py index babd57888..f181dafa5 100644 --- a/python/sglang/srt/utils/common.py +++ b/python/sglang/srt/utils/common.py @@ -3646,3 +3646,13 @@ def get_current_device_stream_fast(): if cached_device_index == -1: cached_device_index = torch.get_device_module().current_device() return torch.get_device_module().current_stream(cached_device_index) + + +def raise_error_or_warn(obj, strict, counter_name, message, log_interval=1000): + if strict: + raise ValueError(message) + else: + count = getattr(obj, counter_name, 0) + if count % log_interval == 0: + logger.warning(message) + setattr(obj, counter_name, count + 1)