diff --git a/python/sglang/srt/session/streaming_session.py b/python/sglang/srt/session/streaming_session.py index 817266f62e..b483e51b76 100644 --- a/python/sglang/srt/session/streaming_session.py +++ b/python/sglang/srt/session/streaming_session.py @@ -280,9 +280,9 @@ class StreamingSession: def _free_kv_aligned(self, kv: ReqKvInfo, target: int, end: int) -> None: """Free [ceil_align(target), end): paged free returns whole pages, so the partial page stays until release_session.""" - if end <= target: - return start = target if self.cache.page_size > 1: start = ceil_align(start, self.cache.page_size) + if end <= start: + return self.cache.free_kv_row(kv, [(start, end)]) diff --git a/test/registered/unit/mem_cache/test_streaming_session_unit.py b/test/registered/unit/mem_cache/test_streaming_session_unit.py index 11949f6951..148bdb8a5a 100644 --- a/test/registered/unit/mem_cache/test_streaming_session_unit.py +++ b/test/registered/unit/mem_cache/test_streaming_session_unit.py @@ -218,6 +218,24 @@ def test_trim_overshoot_postcondition(): assert [t.tolist() for t in allocator.freed] == [[38, 39, 40, 41], [42, 43]] +@pytest.mark.parametrize( + "target, end, freed", + [(17, 20, []), (17, 32, []), (17, 40, [(32, 40)]), (32, 48, [(32, 48)])], +) +def test_free_kv_aligned_skips_a_range_inside_the_partial_page(target, end, freed): + """Rounding ``target`` up to a page boundary can move the start of the + range at or past ``end``; such a rewind frees nothing and must not reach + the cache at all, while a range that still spans a whole page is freed.""" + calls = [] + session = StreamingSession( + SimpleNamespace( + page_size=16, free_kv_row=lambda _kv, ranges: calls.extend(ranges) + ) + ) + session._free_kv_aligned(None, target, end) + assert calls == freed + + @pytest.mark.parametrize("operation", ["trim", "match"]) @pytest.mark.parametrize("component", [ComponentType.SWA, ComponentType.AUXILIARY_SWA]) def test_session_rewind_keeps_component_cursors_page_aligned(operation, component):