fix(serve): auto-generate batch completion sessions to avoid id collision (#4773)#4774
Open
Anai-Guo wants to merge 1 commit into
Open
fix(serve): auto-generate batch completion sessions to avoid id collision (#4773)#4774Anai-Guo wants to merge 1 commit into
Anai-Guo wants to merge 1 commit into
Conversation
…sion (InternLM#4773) The legacy /v1/completions list-prompt branch assigned one session per prompt with hardcoded user ids 1, 2, 3... via create_session(i + 1). map_user_session_id raises when a user id is already mapped, so two concurrent batch requests (even single-item lists with the default session_id) both requested id 1 and the second crashed with an unhandled ValueError -> 500. Create batch sessions the same way the str branch already does for the common unset session_id=-1 case: auto-generate unique internal ids. A single-item list now mirrors the str path (honors an explicit session_id); multi-prompt batches get one auto-generated session each. This removes the collision entirely and keeps every prompt distinct.
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.
Summary
Fixes #4773.
The legacy
/v1/completionsendpoint assigns one session per prompt. Whenrequest.promptis a list,completions_v1created sessions with hardcoded user ids1, 2, 3...:create_session(i + 1)routes throughSessionManager.map_user_session_id, which raises when a user id is already mapped. So two concurrent list-prompt requests — even single-item lists like["hello"]with the defaultsession_id— both request internal id1, and the second crashes with an unhandledValueError: User session id 1 already exists, surfacing as a 500. This branch is the only one of the fourcreate_sessioncall sites (:440,:830,:986) that ignoresrequest.session_idand bypasses the user→internal mapping semantics added in #4523.Fix
Create batch sessions the way the
strbranch already does for the common unsetsession_id=-1case: auto-generate unique internal ids (viacreate_session()/session_mgr.get()), which never touch the user map and therefore cannot collide across concurrent requests.strpath exactly — honors an explicitsession_id, or auto-generates when it's the default-1.Downstream code only uses each session as an opaque handle (
generate(prompt, session, ...), cleaned up by_with_request_cleanup), so the internal ids need not be contiguous.Test
Added
test_batch_completions_sessions_auto_generate_without_collision(extends the existingSessionManager-level suite, no GPU/model needed). It reproduces the reporter's minimal repro — mapping a fixed user id twice raises — and asserts that auto-generation hands out distinct, non-colliding ids for a batch of prompts.🤖 Generated with Claude Code