{"record":{"id":"4d970b133dfbe6f1","repo":"sgl-project/sglang","slug":"invalid-stream-index-idx","errorCode":null,"errorMessage":"Invalid stream index: {idx}","messagePattern":"Invalid stream index: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/multiplex/pdmux_context.py","lineNumber":147,"sourceCode":"    STREAM_GROUPS.append(\n        (torch.cuda.Stream(gpu_id), torch.cuda.Stream(gpu_id))\n    )  # Normal stream for prefill\n    for prefill_sm, decode_sm in divisions:\n        STREAM_GROUPS.append(\n            (spatial.create_greenctx_stream_by_value(prefill_sm, decode_sm, gpu_id))\n        )\n    STREAM_GROUPS.append(\n        (torch.cuda.Stream(gpu_id), torch.cuda.Stream(gpu_id))\n    )  # Normal stream for decode\n\n    CURRENT_STREAM_IDX = 0\n    CURRENT_STREAM_GROUP = STREAM_GROUPS[CURRENT_STREAM_IDX]\n\n\ndef set_current_stream_idx(idx: int):\n    global CURRENT_STREAM_IDX, CURRENT_STREAM_GROUP\n    if idx < 0 or idx >= len(STREAM_GROUPS):\n        raise ValueError(f\"Invalid stream index: {idx}\")\n    CURRENT_STREAM_IDX = idx\n    CURRENT_STREAM_GROUP = STREAM_GROUPS[CURRENT_STREAM_IDX]\n\n\ndef get_stream_groups() -> list[tuple[torch.cuda.Stream, torch.cuda.Stream]]:\n    \"\"\"Get the stream groups.\"\"\"\n    return STREAM_GROUPS\n\n\ndef get_sm_counts() -> list[tuple[int, int]]:\n    \"\"\"Get the SM counts.\"\"\"\n    return SM_COUNTS\n\n\ndef get_current_stream_idx() -> int:\n    \"\"\"Get the current stream index.\"\"\"\n    return CURRENT_STREAM_IDX\n","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/multiplex/pdmux_context.py#L129-L165","documentation":"set_current_stream_idx switches the process-global current stream group used for PD multiplexing; the index must address STREAM_GROUPS. Out-of-range indices indicate bookkeeping bugs (e.g. using sm_group_num instead of group index, off-by-one).","triggerScenarios":"Calling set_current_stream_idx(idx) with idx < 0 or idx >= len(STREAM_GROUPS), e.g. passing a group count or a rank number instead of an index.","commonSituations":"adjust_stream_groups logic passing sm_group_num as the last index (should be sm_group_num-1); index computed from a differently-sized list after config change.","solutions":["Use idx in range [0, len(STREAM_GROUPS)-1]; for the last group use len(STREAM_GROUPS)-1","Validate idx against get_stream_groups() length before setting","Re-check whether you meant to pass an index vs a count"],"exampleFix":"# before\nset_current_stream_idx(sm_group_num)  # off-by-one\n# after\nset_current_stream_idx(len(get_stream_groups()) - 1)","handlingStrategy":"validation","validationCode":"groups = get_stream_groups()\nassert 0 <= idx < len(groups), f'idx must be in [0, {len(groups)-1}]'\nset_current_stream_idx(idx)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never pass group counts where indices are expected","Centralize stream switching in one helper that bounds-checks"],"tags":["pdmux","stream","index-validation"],"backgroundTag":"index-out-of-range","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}