Repository navigation
Conversation
Co-authored-by: Cheng Wan <54331508+ch-wan@users.noreply.github.com> (cherry picked from commit 2862173) Backport notes for feature/optimize_main: - The tokenizer_manager.py change is identical to upstream: validate every rid of a (batch) request before inserting any ReqState, so a batch whose later rid collides with an in-flight request no longer leaves orphan rid_to_state entries (which made those rids unusable and kept graceful SIGTERM shutdown waiting forever). - Tests adapted to this branch's helpers: import `patch`; drop the LoRA adapter-refcount assertions (`_make_tm_for_generate` here has no `enable_lora`, and TokenizerManager has no `_release_lora_once`); drop the `ReqState.dispatched` assertion (field not present on this branch). - test_tokenizer_manager_rid_cleanup.py: 24 passed with this change; on the parent commit the new cases fail (11 failed), including rid collisions at batch positions 1 and 2.
This was referenced Oct 21, 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
批量请求(
rid为列表)中,如果排在后面的某个 rid 与一个仍在处理中的请求重名,_init_req_state会先把前面的 rid 逐个写进rid_to_state,检查到重名时才抛出Duplicate request ID detected。而_init_req_state在generate_request的try之外调用,前面已写入的状态不会被回滚,形成孤儿:Duplicate request ID detected;rid_to_state清空,会一直等待。我们在泄漏测试中实测复现:每轮构造 3 次,P 侧 tokenizer 的孤儿数 0 → 3 → 6,不回收。
上游 sgl-project/sglang 已于 10-12 修复:#41911「Make tokenizer request-state initialization atomic」(提交
28621739fc)。本 PR 将其回移到feature/optimize_main。Modifications
python/sglang/srt/managers/tokenizer_manager.py:与上游逐行相同。先检查整批 rid(与在途请求或批内重名都拒绝),全部通过后再构造ReqState,最后一次性update进rid_to_state;构造过程中抛异常也不会留下部分状态。test/registered/unit/managers/test_tokenizer_manager_rid_cleanup.py:带入上游新增的TestInitReqStateAtomicity,按本分支的测试辅助函数调整:patch导入;_make_tm_for_generate没有enable_lora参数,TokenizerManager没有_release_lora_once),用例改名为test_live_collision_preserves_map,重名位置 0/1/2、Generate/Embedding 两种请求的检查保留;ReqState.dispatched断言(本分支ReqState无此字段)。cherry picked from commit 28621739fc6e8084b59f0b02178413f993e35afb和回移说明。Accuracy Tests
VERIFIED:CPU 单测,Docker 容器(python 3.10.12、pytest 9.1.1),
PYTHONPATH指向本分支源码,未安装包、未启动服务。PYTHONPATH="$PWD/python" python -B -m pytest -q -p no:cacheprovider \ test/registered/unit/managers/test_tokenizer_manager_rid_cleanup.pytokenizer_manager.py+ 同一测试文件):11 failed、23 passed。批内第 2、3 项与在途请求重名的用例失败(第 1 项重名本来就不会留下孤儿,通过),构造过程中抛异常的用例也失败。--select=F401,F821,UP037):测试文件通过;tokenizer_manager.py第 1220 行附近有一处 black 格式问题,修改前就存在,与本 PR 无关,未改动。UNTESTED:未编包,未做服务级端到端复测(构造批量请求重名后再用同一 rid 请求、SIGTERM 优雅退出)。
Speed Tests and Profiling
未运行;改动只涉及请求登记时的 rid 检查顺序,不涉及模型计算。
Checklist