{"record":{"id":"b2c40fbf72812d35","repo":"sgl-project/sglang","slug":"kv-canary-read-bytes-must-be-non-negative-got-r","errorCode":null,"errorMessage":"kv-canary: read_bytes must be non-negative, got {requested}","messagePattern":"kv-canary: read_bytes must be non-negative, got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/kv_canary/pool_patcher/buffer_alloc.py","lineNumber":51,"sourceCode":"\n\ndef _clip_read_bytes_aligned(*, requested: int, num_bytes_per_token: int) -> int:\n    \"\"\"Validate and clip read_bytes for the CUDA fold kernel's 128-bit aligned loads.\n\n    Normalizes sentinels (``sys.maxsize`` -> ``num_bytes_per_token``, ``0`` -> ``0``) and\n    rejects negative / unaligned / oversized requests.\n    \"\"\"\n    if num_bytes_per_token <= 0 or num_bytes_per_token % _REAL_KV_READ_ALIGN != 0:\n        raise ValueError(\n            \"kv-canary: num_bytes_per_token must be a positive multiple of \"\n            f\"{_REAL_KV_READ_ALIGN}, got {num_bytes_per_token}\"\n        )\n    if requested == 0:\n        return 0\n    if requested == sys.maxsize:\n        return num_bytes_per_token\n    if requested < 0:\n        raise ValueError(f\"kv-canary: read_bytes must be non-negative, got {requested}\")\n    if requested > num_bytes_per_token:\n        raise ValueError(\n            \"kv-canary: read_bytes must be <= num_bytes_per_token \"\n            f\"({num_bytes_per_token}), got {requested}\"\n        )\n    if requested % _REAL_KV_READ_ALIGN != 0:\n        raise ValueError(\n            \"kv-canary: read_bytes must be a multiple of \"\n            f\"{_REAL_KV_READ_ALIGN}, got {requested}\"\n        )\n    return requested\n\n\ndef make_row_source(\n    *,\n    layer_buffer: torch.Tensor,\n    read_bytes: int,\n) -> Tuple[RealKvSource, ...]:","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/kv_canary/pool_patcher/buffer_alloc.py#L33-L69","documentation":"kv-canary's buffer allocation helper rejects negative read_bytes values. _clip_read_bytes_aligned validates the requested per-token read size before allocating canary buffers, and a negative value would corrupt size math, so it fails fast with ValueError.","triggerScenarios":"Calling make_row_source or make_packed_source with a negative read_bytes argument; e.g. read_bytes=-16 or a computed value that underflows (subtraction yielding a negative number).","commonSituations":"Computing read_bytes as num_bytes_per_token - something and passing it unclamped; passing -1 as an 'unset' sentinel instead of sys.maxsize (which is the supported 'use full size' sentinel here).","solutions":["Fix the caller to pass a non-negative read_bytes (0 means read nothing, sys.maxsize means read the full per-token size)","If read_bytes is computed, clamp it: max(0, computed) or guard against underflow before calling make_row_source/make_packed_source","Use sys.maxsize instead of -1 when you want the default full-size read"],"exampleFix":"// before\nsrc = make_row_source(pool, read_bytes=num_bytes_per_token - overshoot)\n// after\nsrc = make_row_source(pool, read_bytes=max(0, num_bytes_per_token - overshoot))","handlingStrategy":"validation","validationCode":"from sglang.srt.kv_canary.pool_patcher.buffer_alloc import _clip_read_bytes_aligned\nif read_bytes < 0:\n    raise ValueError('read_bytes must be >= 0')\nsrc = make_row_source(pool, read_bytes=read_bytes)","typeGuard":"def is_valid_read_bytes(n: int) -> bool:\n    return isinstance(n, int) and 0 <= n <= sys.maxsize","tryCatchPattern":"try:\n    clipped = _clip_read_bytes_aligned(requested=n, num_bytes_per_token=cell)\nexcept ValueError as e:\n    logger.warning('bad read_bytes: %s', e); clipped = 0","preventionTips":["Never pass -1 as a sentinel; use sys.maxsize for 'full size'","Clamp computed read_bytes with max(0, ...)"],"tags":["kv-canary","validation","valueerror","read-bytes"],"backgroundTag":"argument-validation-failed","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}