Skip to content

[Bug] Account for deeply nested media without recursive stack growth #6038

Closed

Description

@Afloat16

Version

billion-context 0.1.213, master 4be21b08ed3da8b69d1800a10a9c0b9c5a707f71.

Problem

Valid 20,000-level request JSON overflows the recursive media-byte walk.

Proposed correction

Traverse pending JSON values iteratively, preserving all existing media-shape accounting.

Regression coverage

20,000 nested containers, non-media text, malformed JSON and mixed media-wire siblings.

Validation

Passed against exact fork commit 01e42ad79402ea62220e7ac60c2d54075da8c4ef in an isolated GitHub Actions checkout: validation job. The targeted regression and related suites, npm run typecheck, and npm run build all passed on Node 22. This is targeted verification, not the full upstream CI matrix.

node --import ./tests/preload-storage-baseline.mjs --import ./tests/preload-test-env.mjs --import tsx --test tests/media-walk-depth.test.ts
npm run typecheck
npm run build

Related work checked

Related #4119, #4382 and #4923 address repricing/maximum-string handling, not traversal depth.

Activity

  1. ranxianglei commented on Nov 9, 2026

    @ranxianglei
    Owner

    🤖 Powered by ework · qwen3.8-27b

    [bot] 🏷 (ai: vllm-qwen/qwen3.8-27b)

    查重:不重复 — the mechanism (recursive walk over client-controlled request JSON overflowing the JS call stack) has no prior issue or PR. Your own PR #6048 already targets this exact thread, so per the one-fix-PR-per-thread rule I am reviewing and completing THAT PR rather than opening a parallel fix. No new issues opened.

    Verified the bug locally (version-first): on master 4be21b08ed (= your reported base, bili 0.1.213), mediaBytesOfWireBody (src/agent/media-bytes.ts) throws RangeError: Maximum call stack size exceeded at 20,000 nesting levels on Node 22 (Linux) — the stack trace lands in the recursive walk at src/agent/media-bytes.ts:30. JSON.parse itself is iterative and survives arbitrarily deep input, so the overflow is purely the walk, not the parse. Your regression test fails 2/3 with exactly this RangeError against the old code and passes 3/3 against your fix — it has teeth.

    Layer judgment: root cause is unbounded recursion over attacker-/client-controlled JSON in the shared doom-math helper — not a missing try/catch at some call site. Your direction (iterative traversal at the source, preserving the structural media-shape accounting) is the correct layer. Call-site exposure varies: wireBodyMediaBytes (opencode-native) already swallows the throw conservatively, while the dsh takeover arms and the shared direct-fallback arm route through bodyOverWindowDetail without a local guard — so fixing at the source is the only complete fix.

    One adjacent instance of the SAME mechanism found during review (same symptom, same recursion-over-client-JSON pattern, different file — proxy side, not agent lane): estimateRawBodyTokens and estimateRawBodyBreakdown in src/preflight.ts:453/:487 also walk the parsed client body recursively, and both throw RangeError at 10,000 levels (verified locally). They sit on the proxy's MAIN request path (src/server/handle.ts:1688 stamps rawInputTokens for every proxy-mode request) plus the side-request guard (side-request.ts:740) and threads-lane. Leaving them recursive means this exact input class still crashes proxy-mode requests after #6048 merges. Per the one-mechanism-one-PR rule I am folding them into PR #6048 as a separately-labeled commit (same package, no kernel boundary involved) with matching depth regression coverage.

    Status: full review of #6048 in progress (code/tests/regression sweep done, CI green so far: 0 failed / 6 pending). Structured conclusion follows in-thread once the preflight commit lands and CI re-runs.

  2. ranxianglei commented on Nov 9, 2026

    @ranxianglei
    Owner

    🤖 Powered by ework · qwen3.8-27b

    (ai: vllm-qwen/qwen3.8-27b)

    Review complete — your PR #6048 is merge-ready. CI on the pushed head: 0 failed, 12 shards in progress, remaining lanes skipped by env gate (normal). One reviewer-added complement commit landed on your branch (details below); nothing parallel was opened.

    Verified before touching anything

    • Reproduced on master 4be21b08ed: a valid 20,000-level body throws RangeError: Maximum call stack size exceeded inside the recursive walk in src/agent/media-bytes.ts. JSON.parse itself is iterative and survives 200k levels — the overflow is purely the hand-written recursive walks, exactly as you diagnosed.
    • Exposure varies by call site: the opencode-native lane wraps the call in try/catch (degrades to 0, conservative-safe); the dsh takeover arms and the shared direct-fallback arm (bodyOverWindowDetail, src/agent/native-intercept.ts) call it unguarded.

    Independent review of your commit
    Sound. The iterative explicit-stack DFS visits exactly the node set the recursive version did; the byte sum is order-independent; all seven media-shape checks are unchanged; naming/style matches the file; no dead code; coverage is two-directional (deep hit, deep miss, malformed JSON, shallow sibling matrix). Your depth test has real teeth: run against the old code it fails 2/3 with RangeError.

    Reviewer-added complement — commit ab67515c59, pushed to your branch
    The same mechanism existed in two more walks: estimateRawBodyTokens and estimateRawBodyBreakdown in src/preflight.ts enumerate every string leaf of the parsed client body on the proxy MAIN path (handle.ts rawInputTokens stamping), the side-request guard, and threads-lane — and both overflow identically at ~10–20k depth. Without them, proxy-mode users still crash on the same input class this issue describes. Both converted to the same iterative pattern, preserving key inheritance, the NON_TEXT_BODY_KEYS exclusion set, top-level bucketing and the total invariant byte-for-byte. New suite tests/raw-body-walk-depth.test.ts: 20k-level object and array chains with media-shaped bottoms (verified to fail with RangeError against master's preflight.ts, pass on the fix), plus shallow plumbing pins (exclusion set, key inheritance through arrays, bucket split, non-object top-level).

    Verification on the pushed head

    • Targeted suites covering all three walks: 141/141 pass (media-walk-depth, raw-body-walk-depth, dsh-native, session-gc, side-request-isolation)
    • npm run typecheck clean, npm run build OK
    • The platform's ci-check cherry-pick applied cleanly onto current master ⇒ the branch rebases conflict-free (drift commits touch unrelated files)

    Behavior-change disclosure: none beyond removing the crash — token counts, buckets and byte accounting are identical for every body that previously worked; no config/env/wire changes.

    标签: issue = bug, scope/wiring, p2(边缘健壮性缺陷:仅 ≥1 万层病态嵌套触发,真实流量 2–4 层,p1 标准不满足);PR #6048 already carried the same three labels.

    One adjacent instance sits outside this PR's package boundary — flagged in my next comment for owner direction.

    八问(结构化结论)

    1. 是什么问题: bili 用递归遍历客户端可控的请求 JSON(媒体字节统计 + raw-body token 估算),嵌套超过约 1 万层的合法请求栈溢出崩溃(RangeError),该请求被拒。
    2. 什么原因导致的: 根因是三个共享 helper 对客户端输入做无界递归(media-bytes.ts 一处、preflight.ts 两处);JSON.parse 本身迭代、可扛 20 万层——溢出完全来自手写递归遍历,不是解析器问题。
    3. 影响范围与严重程度: 仅病态深嵌套(≥1 万层)请求触发。各路径暴露不一:opencode-native 车道 try/catch 兜底降级为 0(安全);dsh takeover / direct-fallback 臂与代理主路径(handle.ts rawInputTokens 打点、side-request guard)无保护 → 请求直接失败。p2 边缘健壮性缺陷,非全面不可用。
    4. 怎么修复的: 三处遍历统一改为迭代显式栈,语义逐字节保持(键继承、排除集、顶层分桶、total 不变量均不变);新增深度回归测试(20k 层对象链/数组链,实证旧代码失败、新代码通过)+ 浅层语义钉死测试。
    5. 是否有回归问题: 无。既有行为逐项核对:计数/分桶/字节账对一切原本能工作的输入逐字节相同(浅层矩阵钉住排除集、数组键继承、分桶、非对象顶层);定向套件 141/141 绿;typecheck/build 干净;ci-check 干净落 master 证明可无冲突 rebase。
    6. 是否破坏缓存: 不涉及——未触碰缓存/重试/游标/去重逻辑;rawInputTokens 打点值对原有输入不变。
    7. 是否改了设计方向: 不涉及——纯遍历机制转换,模块边界、调用点、协议形状均未动。
    8. 是否引入了新的配置: 不涉及——无新 env/config/默认值。

    判定: 可合(待 CI 全绿后由人工合并;按规矩我不执行合并)。

  3. ranxianglei commented on Nov 9, 2026

    @ranxianglei
    Owner

    🤖 Powered by ework · qwen3.8-27b

    🔶 需要决策
    (ai: vllm-qwen/qwen3.8-27b)

    背景一句话: 分析 #6038 时发现内核侧还有一处同机制实例(对客户端可控 JSON 的无界递归遍历),不在 PR #6048 的包边界内,未动。

    待决问题: 是否修复 kernel/src/media-refs.ts:159 —— extractMediaRefs() 内的 walk(parts) 沿 google-wire 嵌套 functionResponse.parts 无限递归,经 collectFoldMediaRefs ← kernel/src/compress.ts:44 可达;20k 层嵌套的 functionResponse 链会同样栈溢出。旁证:kernel/src/protected.ts:715-728 的同形 Google 分支只下探一层(安全),说明这里是漏改而非有意设计。

    选项(各带代价):

    • A. 按 2026-11-06 mechanical-bug 豁免直接修:与宿主侧已合入本线程的修法机械同构(迭代显式栈+复现测试+kernel-change: 声明+版本 bump),人审仍 gating 合并。代价:多一个 kernel PR、走 kernel 版本节奏。
    • B. 搁置:google-wire 超深嵌套会话在该路径继续崩溃,等真实流量出现再修。代价:同类输入在 google 车道持续失败,且宿主侧已修、内核侧未修的不对称会留到下一次排查才被注意到。

    推荐: A——修法已在本线程被实证安全(同一转换模式、同一测试配方),无设计取舍成分,符合豁免条款"显而易见机械性缺陷"。

    不决定的默认行为: 保持搁置(B),本条记录留在本线程,不动 kernel/。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions