Skip to content

fix(serve): auto-generate batch completion sessions to avoid id collision (#4773)#4774

Open
Anai-Guo wants to merge 1 commit into
InternLM:mainfrom
Anai-Guo:fix-completions-list-session-4773
Open

fix(serve): auto-generate batch completion sessions to avoid id collision (#4773)#4774
Anai-Guo wants to merge 1 commit into
InternLM:mainfrom
Anai-Guo:fix-completions-list-session-4773

Conversation

@Anai-Guo

Copy link
Copy Markdown

Summary

Fixes #4773.

The legacy /v1/completions endpoint assigns one session per prompt. When request.prompt is a list, completions_v1 created sessions with hardcoded user ids 1, 2, 3...:

elif isinstance(request.prompt, list):
    for i in range(len(request.prompt)):
        sessions.append(VariableInterface.create_session(i + 1))

create_session(i + 1) routes through SessionManager.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 default session_id — both request internal id 1, and the second crashes with an unhandled ValueError: User session id 1 already exists, surfacing as a 500. This branch is the only one of the four create_session call sites (:440, :830, :986) that ignores request.session_id and bypasses the user→internal mapping semantics added in #4523.

Fix

Create batch sessions the way the str branch already does for the common unset session_id=-1 case: auto-generate unique internal ids (via create_session() / session_mgr.get()), which never touch the user map and therefore cannot collide across concurrent requests.

  • A single-item list now mirrors the str path exactly — honors an explicit session_id, or auto-generates when it's the default -1.
  • A multi-prompt batch gets one auto-generated session per prompt, keeping every prompt distinct from each other and from other concurrent requests.

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 existing SessionManager-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

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Legacy /v1/completions list-prompt path ignores request.session_id, crashes under concurrency

1 participant