diff --git a/python/sglang/srt/managers/schedule_batch.py b/python/sglang/srt/managers/schedule_batch.py index 3541eef624..3656fa9c6c 100755 --- a/python/sglang/srt/managers/schedule_batch.py +++ b/python/sglang/srt/managers/schedule_batch.py @@ -1110,8 +1110,10 @@ class Req(ReqDllmMixin): self._think_end_matcher: Optional[TokenSequenceMatcher] = None self._think_end_match_len = 0 - # Sampling info - if isinstance(sampling_params.custom_params, dict): + # Only custom logit processors need the "__req__" reference cycle. + if custom_logit_processor is not None and isinstance( + sampling_params.custom_params, dict + ): sampling_params = copy.copy(sampling_params) sampling_params.custom_params = sampling_params.custom_params | { "__req__": self diff --git a/python/sglang/srt/sampling/sampling_params.py b/python/sglang/srt/sampling/sampling_params.py index 29ba3075f3..286003a9a2 100644 --- a/python/sglang/srt/sampling/sampling_params.py +++ b/python/sglang/srt/sampling/sampling_params.py @@ -25,10 +25,8 @@ try: except ImportError: import sre_parse # Python < 3.11 -# JSON-safe value types for custom_params. Must survive msgpack IPC -# without PickleWrapper. After deserialization on the scheduler side, -# Req.__init__ injects "__req__" (a Req object) into the dict in-process; -# that augmented dict is never re-serialized. +# JSON-safe value types for custom_params. Must survive msgpack IPC +# without PickleWrapper. _JsonScalar = Union[None, bool, int, float, str] CustomParamValue = Union[ _JsonScalar, diff --git a/test/registered/unit/managers/test_req_custom_params_ref.py b/test/registered/unit/managers/test_req_custom_params_ref.py new file mode 100644 index 0000000000..37f0c74a9f --- /dev/null +++ b/test/registered/unit/managers/test_req_custom_params_ref.py @@ -0,0 +1,45 @@ +import gc +import unittest +import weakref +from array import array + +from sglang.srt.managers.schedule_batch import Req +from sglang.srt.sampling.sampling_params import SamplingParams +from sglang.test.ci.ci_register import register_cpu_ci +from sglang.test.test_utils import CustomTestCase + +register_cpu_ci(est_time=2, suite="base-a-test-cpu") + + +def _make_req(custom_logit_processor=None): + return Req( + rid="r", + origin_input_text="", + origin_input_ids=array("q", [1, 2]), + sampling_params=SamplingParams(custom_params={"thinking_budget": 8}), + custom_logit_processor=custom_logit_processor, + ) + + +class TestReqCustomParamsRef(CustomTestCase): + def test_req_without_a_processor_is_freed_by_refcount(self): + """custom_params without a custom logit processor must not make the + request reference itself; refcount alone frees it.""" + gc.disable() + try: + req = _make_req() + self.assertNotIn("__req__", req.sampling_params.custom_params) + ref = weakref.ref(req) + del req + self.assertIsNone(ref()) + finally: + gc.enable() + + def test_processor_reads_its_request(self): + """A custom logit processor still finds its request under __req__.""" + req = _make_req(custom_logit_processor="{}") + self.assertIs(req.sampling_params.custom_params["__req__"], req) + + +if __name__ == "__main__": + unittest.main()