{"record":{"id":"758856fbe52a1044","repo":"sgl-project/sglang","slug":"temp-set-env-should-not-be-used-for-sglang-env-var","errorCode":null,"errorMessage":"temp_set_env should not be used for sglang env vars","messagePattern":"temp_set_env should not be used for sglang env vars","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"warning","filePath":"python/sglang/srt/utils/common.py","lineNumber":1284,"sourceCode":"        return default\n    try:\n        return int(value)\n    except ValueError:\n        return default\n\n\n@contextmanager\ndef temp_set_env(*, allow_sglang: bool = False, **env_vars: Any):\n    \"\"\"Temporarily set environment variables, restoring originals on exit.\n\n    By default, SGLANG_*/SGL_* keys are rejected — use ``Envs`` descriptors\n    for those.  Pass ``allow_sglang=True`` only for special env vars that\n    intentionally bypass ``environ.py``.\n    \"\"\"\n    if not allow_sglang:\n        for key in env_vars:\n            if key.startswith(\"SGLANG_\") or key.startswith(\"SGL_\"):\n                raise ValueError(\"temp_set_env should not be used for sglang env vars\")\n\n    backup = {key: os.environ.get(key) for key in env_vars}\n    try:\n        for key, value in env_vars.items():\n            if value is None:\n                os.environ.pop(key, None)\n            else:\n                os.environ[key] = str(value)\n        yield\n    finally:\n        for key, value in backup.items():\n            if value is None:\n                os.environ.pop(key, None)\n            else:\n                os.environ[key] = value\n\n\ndef support_triton(backend: str) -> bool:","sourceCodeStart":1266,"sourceCodeEnd":1302,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/utils/common.py#L1266-L1302","documentation":"SGLang's test/helper utility temp_set_env refuses to set environment variables whose names start with SGLANG_ or SGL_. SGLang env vars are managed centrally through python/sglang/srt/environ.py, and ad-hoc mutation would bypass parsing, defaults, and deprecation handling.","triggerScenarios":"Calling temp_set_env({'SGLANG_X': '1'}) or with any SGL_/SGLANG_-prefixed key without allow_sglang=True. Commonly hit in tests (test_from_env_bool, test_from_env_str) and in weight-loading or eval helpers that try to toggle features via env.","commonSituations":"Writing new tests that flip an SGLANG_ feature flag; refactoring code that previously used os.environ directly; adding a genuinely special env var that intentionally bypasses environ.py.","solutions":["Register/define the variable in python/sglang/srt/environ.py and set it via the proper mechanism instead of temp_set_env","If the var is intentionally outside environ.py, pass allow_sglang=True explicitly","Prefer injecting configuration through ServerArgs/function parameters rather than env mutation"],"exampleFix":"# before\ntemp_set_env({\"SGLANG_DISABLE_TOKENIZER_BATCH\": \"1\"})\n# after\ntemp_set_env({\"SGLANG_DISABLE_TOKENIZER_BATCH\": \"1\"}, allow_sglang=True)\n# or better: use the environ.py accessor in test","handlingStrategy":"validation","validationCode":"def safe_temp_set_env(env_vars: dict) -> None:\n    bad = [k for k in env_vars if k.startswith(('SGLANG_', 'SGL_'))]\n    if bad and not getattr(safe_temp_set_env, 'allow_sglang', False):\n        raise ValueError(f'register {bad} in environ.py instead')\n    temp_set_env(env_vars, allow_sglang=getattr(safe_temp_set_env, 'allow_sglang', False))","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Define all SGLANG_* vars in python/sglang/srt/environ.py","Use allow_sglang=True only with a comment explaining the bypass","Prefer ServerArgs parameters over env toggles in tests"],"tags":["environment-variables","testing","conventions","sglang"],"backgroundTag":"env-var-misuse","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}