Fix token leak with logprob_start_len=0 in streaming sessions#20557
Merged
hnyls2002 merged 3 commits intosgl-project:mainfrom Mar 19, 2026
Merged
Fix token leak with logprob_start_len=0 in streaming sessions#20557hnyls2002 merged 3 commits intosgl-project:mainfrom
hnyls2002 merged 3 commits intosgl-project:mainfrom
Conversation
When logprob_start_len=0, init_next_round_input truncates the prefix match key to length 0. For streaming sessions this bypasses the slot's committed KV, orphaning allocated tokens. Fix: in SessionAwareCache.match_prefix, use fill_ids length instead of the truncated key to determine the prefix, and clamp logprob_start_len to the prefix length so the scheduler doesn't expect logprobs for tokens already in the session's committed KV. Also adds test_session_logprob_leak.py covering sessions with various logprob configurations and bitwise-exact logprob comparison against non-session baseline.
Contributor
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
Wangzheee
pushed a commit
to Wangzheee/sglang
that referenced
this pull request
Mar 21, 2026
0-693
pushed a commit
to 0-693/sglang
that referenced
this pull request
Mar 25, 2026
dutsc
pushed a commit
to dutsc/sglang
that referenced
this pull request
Mar 30, 2026
JustinTong0323
pushed a commit
to JustinTong0323/sglang
that referenced
this pull request
Apr 7, 2026
yhyang201
pushed a commit
to yhyang201/sglang
that referenced
this pull request
Apr 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
When
logprob_start_len=0is used with streaming sessions,init_next_round_inputtruncates the prefix match key to length 0. For streaming sessions,SessionAwareCache.match_prefixthen computesprefix_len = min(kv_committed_len, 0) = 0, bypassing the session slot's committed KV and orphaning allocated tokens. This causes a token memory leak that crashes the server.Modifications
In
SessionAwareCache.match_prefix, usefill_idslength (the full token history for the session) instead of the truncated key to determine the prefix match length. Also clamplogprob_start_lenup to the prefix length so the scheduler doesn't expect logprobs for tokens already in the session's committed KV.Also adds
test_session_logprob_leak.pycovering sessions with:return_logprob=True, defaultlogprob_start_len=-1)logprob_start_len=0)Each test includes bitwise-exact logprob comparison against a non-session baseline.
Checklist