Skip to content

decision request: upstream TTFB idle budget — split header/body phases, which shape? #5831

Open

Description

@ranxianglei

来源: #5807 分析「上游首字节空闲沿用 12 分钟默认值 → 卡死白等」时发现
查重: 无(open+closed 全文检索 TTFB / idle budget / header timeout / "not retried";无同机制条目。相关历史: #1706/#1714 退役 BILI_STREAM_STALL_MS、#551 undici 上限、#1688 主路径 transport 重试、#3148 熔断器)

要拍板什么

fetchWithTimeout 用同一个空闲预算(network.upstreamTimeoutMs,默认 12 分钟)管两个阶段——首字节(TTFB)与收流中途静默——且两个阶段超时都归为同一个失败类 upstream-timeout(刻意不可重试)。网关「保持连接但一直静默」时,整个请求被挂住最多 12 分钟,失败后宿主还要重传完整 prompt(报告者实测 51.3 MiB)。修复方向全部触及重试语义和/或配置面,按 Configuration Surface Discipline 与 §7.4(默认值变更属产品决策)需要 owner 签字。

已核实事实(master @ daa082e,bili 0.1.212,与 #5807 描述逐条对照成立)

  • src/fetch-util.ts:24 UPSTREAM_TIMEOUT_MS = 12*60*1000;fetchWithTimeout(L169-271)初始 arm 覆盖 TTFB、每个 body chunk rearm,两阶段共用同一个 effective(L180-202)。
  • src/upstream-fail.ts:236-238 isFailFastUpstreamKind 不含 upstream-timeout → 无内部重放;hint 写明 "not retried by design"。
  • fetchWithRetry 文档注释(fetch-util.ts L396-401):不重试是刻意的——"to avoid stacking the 12-min idle budget across attempts"。
  • 代码注释(fetch-util.ts L60-70):长预算存在的原因 = 本地模型会合法地静默数分钟(thinking、长 prefill),并明确写 "Do not re-add a shorter timer here"——该指令针对 body 阶段,但单计时器结构使任何全局缩短都会连带打到 TTFB/prefill。
  • 分类层无法区分 header 阶段与 body 阶段的超时(都表现为 AbortError → upstream-timeout);知道是哪个计时器触发的是 fetchWithTimeout 自己,但它没有把阶段信息记到 error 上(方案 A 需要补这条接线)。
  • upstream-timeout 不在 ALERT_KINDS(upstream-alerts.ts L46-54,注释:连接已建立,说 "cannot reach" 是谎话)→ 永久卡死的上游不会触发 [bug] 上游在会话中途不可用时,bili 把一次故障放大成四重代价:固定节奏 20 连击全部冷发、输出预算被静默钳制 83 次(最狠 65536→1360)、紧急收缩与 nudge 停摆均不可见 #3148 熔断器快速失败。

选项

A — 只拆失败类型(零配置面)

header 阶段超时 → 新类 upstream-headers-timeout 加入 isFailFastUpstreamKind(代理内部重放,1.5s/3s 退避,≤3 次);body 阶段维持截断不重放。

  • 收益:零配置变更;已把 upstreamTimeoutMs 调小的人立刻从「宿主重传」变成「代理内重放」。
  • 代价:默认 12 分钟预算下,彻底死掉的端点最终失败时间从 12 分钟变 3×12≈36 分钟(且该类不进 ALERT_KINDS,熔断器救不了);默认场景(卡住但连接活着)仍要先白等 12 分钟才进入第一次重放。
  • 结论:单独不够,与 C 组合后才成立。

B — 硬编码 120 s TTFB 上限(零配置面)

headerBudget = Math.min(effective, 120_000),body 不变。

C — 新增 network.headerIdleTimeoutMs + A(推荐)

unset = 沿用现有 upstreamTimeoutMs 预算 ⇒ 所有存量部署逐字节零行为变化;显式设置(如 30–60 s)⇒ TTFB 有独立短预算,且(配合 A)以可重放的 pre-response 失败收尾。

D(本轮不做,后续单议)— 移动默认值

例如不设字段时 TTFB 默认 60–120 s。本轮否决:会改变长 prefill 本地模型用户的默认行为;若要做,应在 C 落地、字段成为逃生阀之后。

推荐

C + A 一个 PR 闭环:拆 kind + 新字段,字段 unset = 现状逐字节一致。机制先落地,默认值留给后续单独决策。B 有回归否决;A 单独有 3× 最坏代价否决;D 顺延。

请拍板

A) 批准 C+A(新字段在此记录签字)· B) 只批 A · C) 其他(请说明)。决定前不动手实现(配置面 + 重试语义均属 owner 领域)。

Activity

  1. ranxianglei commented on Nov 6, 2026

    @ranxianglei
    OwnerAuthor

    🤖 Powered by ework · qwen3.8-27b

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

    🔶 需要决策

    背景一句话:上游网关「保持连接但一直静默」时,一个请求被同一个 12 分钟空闲预算挂到死,失败后归为不可重试的 upstream-timeout,宿主被迫重传完整 prompt(#5807 实测 51.3 MiB)。根因是「首字节 TTFB」和「收流中途静默」两个阶段共用一个预算、一个失败类。

    查重:issue 已声明无同机制条目(open+closed 全文检索 TTFB/idle budget/header timeout);我复核当前 master(c3dfbe6b2)确认无在途 [PR]/issue 触及 TTFB/header-idle——不重复。

    我已把 issue 里每条代码事实对当前 master 逐条独立复核,全部成立:

    • src/fetch-util.ts:24 UPSTREAM_TIMEOUT_MS = 12*60*1000;:180 单一 effective 管两阶段、:191 初始 arm 覆盖 TTFB、:192-202 每个 chunk rearm(rearm 只在 chunk 上触发,不在 fetch() resolve)。
    • src/upstream-fail.ts:236-238 isFailFastUpstreamKind 不含 upstream-timeout → 无内部重放;:244 hint 写明 "not retried by design"。
    • src/fetch-util.ts:393-401 fetchWithRetry 注释:不重试是刻意的,避免「12 分钟预算叠乘」。
    • src/upstream-alerts.ts:46-54 ALERT_KINDS 不含 upstream-timeout(注释:连接已建立,说 cannot reach 是谎话)→ 卡死端点不会触发 [bug] 上游在会话中途不可用时,bili 把一次故障放大成四重代价:固定节奏 20 连击全部冷发、输出预算被静默钳制 83 次(最狠 65536→1360)、紧急收缩与 nudge 停摆均不可见 #3148 熔断快速失败。
    • 分类层把 header/body 超时都塌缩成同一种(src/upstream-fail.ts:82 AbortError、:86 UND_ERR_*_TIMEOUT → upstream-timeout),只有 fetchWithTimeout 知道是哪个阶段 → 方案 A 需补这条「阶段」接线。
    • src/knobs.ts:80-84 / src/config.ts:2220 的 network 块支持按既有约定加 headerIdleTimeoutMs()(env BILI_UPSTREAM_HEADERS_TIMEOUT_MS > 文件 > undefined)。

    待决问题:拆成「header 短预算 + body 维持 12 分钟 + 新可重放失败类」,字段 unset = 现状逐字节一致——是否批准?这同时动配置面和重试语义,属你签字范围,决定前我不动手。

    选项各带代价:

    方案 配置面 默认部署行为 死端点最坏时间 长 prefill(>120s) 判定
    A 只拆 kind 无 变(可重放) ~3×12≈36 分钟 ↑ 不受影响 单独否决
    B 硬编码 120s TTFB 无 变 快 被杀 否决(回归)
    C+A 拆预算+新字段 新增 1 字段 不变(unset=现状) opt-in 后秒级 不受影响(handoff) ✅ 推荐
    D 移动默认值 复用 C 字段 变 视默认值 不受影响(若走 handoff) 顺延(数据未足)

    我的三处独立判断(不只是复述 issue):

    1. A 单独否决的理由被量化确认:默认 12 分钟下,彻底死掉的静默端点最终失败从 12 分钟拉到 ~36 分钟(≤3 次重放 × 每次等满预算),反而拖累默认部署的死端点场景。A 只有在 C 给了短 header 预算后才转正。
    2. headers-arrival handoff 是 C 的承重墙,且它重写了 D 的风险定性:流式 LLM API 里 HTTP headers 到达很快(服务端接受+开流),真正长的是「headers→首个 chunk」(prefill/thinking)。所以短 header 预算只该约束 connect+headers,在 fetch() resolve 切回 12 分钟 body 预算。据此 D 的真正风险不是「杀长 prefill」(那是 B 的专属缺陷——B 没有 handoff,120s 帽会罩住 prefill 窗口);C 落地后短的 header 默认值会把 prefill 留在 body 预算上。D 该顺延的真实理由是数据不足(有些端点在高负载下合法延迟 headers>120s,分布未知)——先落 C 当逃生阀、测真实 header 延迟,再定有数据支撑的默认值。
    3. 「unset=逐字节不变」有一处微差要如实标注:C 在 unset 时会在 headers 到达处多一次 rearm(现在只在 chunk 上 rearm),headerBudget===bodyBudget===12min 时这把 12 分钟时钟在 headers 处重置,prefill 容限放宽几秒(fetch-start→headers 量级)。方向安全(更宽松)、可忽略,但严格说不是「逐字节相同」。不影响推荐,先讲清楚免得日后意外。

    另两处与 issue 一致:新 kind 先不进 ALERT_KINDS(最小语义,避免卡死端点误触 #3148 熔断,有数据再议);undici directDispatcher 现 headersTimeout=bodyTimeout=同值(fetch-util.ts:89),拆分后须映射 headersTimeout=headerBudget / bodyTimeout=bodyBudget,否则传输层先于看门狗触发(#551 老坑)。

    八问:

    1. 是什么问题:上游卡住但连接存活时,请求被单一 12 分钟空闲预算挂满且失败不可重试,宿主重传全量 prompt。
    2. 什么原因导致:TTFB 与收流静默共用一个计时器预算 + 归入同一不可重试类;分类层把 header/body 超时塌缩成同一种,只有 fetchWithTimeout 知道哪个阶段。
    3. 影响范围/严重程度:默认部署遇「卡住但活」网关都中招(p2,有绕行=调小 upstreamTimeoutMs 但连带压 body 阶段);非 p1(不属会话丢失/缓存泄露两类)。C+A 仅改显式 opt-in 部署,默认零变化;B 会打断 >120s prefill 本地模型(p1 级破坏,故否)。
    4. 怎么修(推荐 C+A):拆两阶段——header 预算管 connect+headers、body 维持 12 分钟、交接点在 fetch() resolve;header 超时→新可重放类 upstream-headers-timeout(pre-response,不双送);新字段 unset=沿用现状。
    5. 回归风险:C+A 默认路径无行为变化(唯一微差=headers 处多一次 rearm,放宽 prefill 容限几秒,安全方向);A 单独死端点最坏 12→~36 分钟(回归);B 杀长 prefill(严重回归)。
    6. 是否破坏缓存:不涉及压缩/前缀缓存机制本身;但重传全量 prompt 会打穿上游前缀缓存并计费——正是本缺陷代价面,C+A 让 opt-in 部署快速失败/内部重放来规避。
    7. 是否改设计方向:是(有意)——把一个全有全无的空闲预算拆成两阶段独立预算+两个失败类;边界清晰,body 阶段语义完全不动。
    8. 是否引入新配置:是——network.headerIdleTimeoutMs(env BILI_UPSTREAM_HEADERS_TIMEOUT_MS),默认 unset=沿用 upstreamTimeoutMs;需 CONFIGURATION.md 中英同步 + web 配置页。这是本次要签字的配置面新增。

    不决定的默认行为:我不碰任何代码(配置面+重试语义是你的领域);线程保持打开等你在 A/B/C 里拍板。拍板后我再按「一个线程一个修复 PR」规则核对在途 PR 并落地实现。

    标签(本镜像 shim 未实现 label 接口,POST /labels 返回 404 'endpoint not implemented in ework shim',无法程序化打标,故在此文字声明):bug · scope/wiring · p2(核心传输层缺陷,非会话丢失/缓存泄露类,且有绕行)。

  2. ranxianglei commented on Nov 8, 2026

    @ranxianglei
    OwnerAuthor

    决策记录(2026-11-08):owner 拍板按"两阶段拆分"推进。分两半落地:

    采纳的关键取舍:首字节超时仅在运维显式把首字节预算收紧到低于空闲预算时才变为可重放新类——这样默认部署(12 分钟)行为逐字节不变,也避开了选项 A 单独使用时"死端点 12 分钟 ×3 ≈ 36 分钟"的代价(只有显式收紧后才存在重放,且此时预算小)。流中途(body)超时维持 upstream-timeout 不重试,#1706/#1714 长静默保证不动。

    #5807 已链接 #5947 为 Closes;本决策 issue 留待 owner 关闭。

  3. ranxianglei commented on Nov 8, 2026

    @ranxianglei
    OwnerAuthor

    🤖 Powered by ework · qwen3.8-27b

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

    收到,决策与推荐方向一致(C+A);而且「条件化重放」这个取舍比我在 issue 里给的原始 C+A 更干净——采纳它是对的:

    分工清楚:预算拆分半已由 #5917 合并;分类 + 代理内重放半在 #5947(挂 #5807)。这两半的实现正确性由各自线程的 review/CI 把关,我不在本决策线程重复审。本 issue 为纯决策记录,按你的说明留待你关闭——我这边不再动代码。

  4. ranxianglei commented on Nov 8, 2026

    @ranxianglei
    OwnerAuthor

    🤖 Powered by ework · qwen3.8-27b

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

    收口确认:本决策线程到此结束。owner 已拍板「两阶段拆分」——预算拆分半由 #5917 交付并合并(新增 BILI_UPSTREAM_FIRST_BYTE_MS,unset = 逐字节零变化),分类 + 代理内重放半在 #5947(Closes #5807)。两半的实现正确性由各自线程的 review/CI 把关。默认部署行为不变,body 中途超时长静默保证(#1706/#1714)保持不动。

    本 issue 为纯决策记录,按你的说明留待你关闭;我这边不再动代码,也不在本线程重复 review。

  5. ranxianglei commented on Nov 8, 2026

    @ranxianglei
    OwnerAuthor

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

    Superseded — the owner decided in the #5807 thread (2026-11-08) and shipped the implementation directly:

    That is the recommended C+A shape plus a refinement that removes the residual risk flagged for A-alone: with the default (unsplit) budgets no marker is ever stamped, so the 3×12-min stacking case cannot occur unless an operator explicitly tightens the knob — and then each attempt is bounded by the tightened budget.

    Nothing left to decide here. Labels updated (p1 removed, superseded added); closing is yours.

  6. ranxianglei commented on Nov 8, 2026

    @ranxianglei
    OwnerAuthor

    🤖 Powered by ework · qwen3.8-27b

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

    收到,确认本决策线程已收口:决策在 #5807 线程拍板,实现由 #5917(已合并)与 #5947(开放、CI 绿、已独立 review)承接;标签 p1→superseded 合理——问题已解决并落地,不再占用高优先级。我这边无任何后续动作:不关闭(留给你)、不动代码、不重复 review。

  7. added a commit that references this issue on Nov 8, 2026
  8. ranxianglei commented on Nov 12, 2026

    @ranxianglei
    OwnerAuthor

    (已收)本单已归入根因聚合 issue(token 见聚合单 body),机器人已停止在本单上的工作,修复走聚合 issue 统一出 PR;本单保留不关,修复验证后统一关闭。(聚合单:[Root Cause] config-knobs (3 items),GitHub #6443)

  9. ranxianglei commented on Nov 21, 2026

    @ranxianglei
    OwnerAuthor

    🤖 Powered by ework · qwen3.8-27b

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

    Merge conflict resolved and force-pushed. PR #5833 is now mergeable: true (the conflict is gone); CI is re-running on the new head.

    What I did this run

    1. Diagnosed the conflict. The PR branch (2026-11-01_offloop-owner-reap, head ffbcf96e846) was 94 commits behind master (98fa9575f10). A git merge-tree dry-run showed exactly one real conflict: scripts/live-files.mjs — both sides added a different LIVE_FILES entry at the same spot (master added tests/storage-backup-repair.test.ts for [Bug][storage] Takeover spawns before reclaim: old and new sidecars simultaneously hold index.db, causing corruption; em | [Bug][storage] 接管时先 spawn 后回收:新旧侧车同时持有 index.db 引发损坏;空 payload_json 又让历史会话静默变 0 #7156; this PR added tests/issue5449-offloop-owner-reap.test.ts for [Bug] 0.1.209 dsh/Windows: an orphaned proxy outlives its dead owner (33 min, ~51% CPU, write lock held) and is never reaped → the replacement drifts 18789→18790 while the caller stalls 826 s with 0 bytes #5449). reference/architecture.md auto-merged cleanly; the other 5 files had no overlap.
    2. Rebased onto master and resolved the single conflict by keeping both array entries (neither dropped); the other 5 commits replayed without conflict.
    3. Verified locally: origin/master is now an ancestor of the new head; diff vs master is exactly the PR's original 7 files (no accidental changes); no conflict markers remain anywhere in the tree; node --check scripts/live-files.mjs passes and LIVE_FILES loads with both entries present (73 total). (TS tests can't run locally — node_modules isn't installed in this environment — so the full-suite verdict is the CI re-run.)
    4. Force-pushed the rebased head ffbcf96e846 → ef645463596 with --force-with-lease, after re-fetching and confirming the remote head was unchanged (no concurrent push to fight).

    Live state (verified this run, not from memory)

    • PR head is now ef645463596, base 98fa9575f10 (master).
    • mergeable: true — conflict resolved. mergeable_state: blocked now only because the fresh CI run on the new head is still in progress (41 check-runs: 31 completed/skipped, 8 queued, 2 in-progress; combined status pending) — not because of a conflict.
    • Note: the mirror check-cache for this PR was stale (last fetched 2026-11-20, not refreshing this run), so I confirmed mergeability + CI state with single one-time reads rather than the cache.

    八问(中文,供人工审阅)

    1. 是什么问题:PR [PR] fix: reap wedged orphaned proxies off-loop when the owner dies (#5449) #5833 与 master 冲突(merge state dirty/blocked),被挡住无法合并。
    2. 什么原因导致的:master 前进了 94 个提交;双方在 scripts/live-files.mjs 的 LIVE_FILES 数组同一位置(storage-integrity-cache.test.ts 之后)各自新增了一个不同条目——master 加 [Bug][storage] Takeover spawns before reclaim: old and new sidecars simultaneously hold index.db, causing corruption; em | [Bug][storage] 接管时先 spawn 后回收:新旧侧车同时持有 index.db 引发损坏;空 payload_json 又让历史会话静默变 0 #7156 的 storage-backup-repair.test.ts,本 PR 加 [Bug] 0.1.209 dsh/Windows: an orphaned proxy outlives its dead owner (33 min, ~51% CPU, write lock held) and is never reaped → the replacement drifts 18789→18790 while the caller stalls 826 s with 0 bytes #5449 的 issue5449-offloop-owner-reap.test.ts,git 无法自动合并同位置的两段新增。
    3. 影响范围与严重程度:仅阻塞合并本身,无任何行为/语义影响——冲突是纯 tripwire 名单数组的同位置双新增。严重度低(合并被挡,非功能故障)。
    4. 怎么修复的:把 PR 分支 rebase 到当前 master,唯一冲突点保留两个条目(谁都不丢),其余 5 个提交自动重放;reference/architecture.md 自动合并无需干预。
    5. 是否有回归问题:无。rebase 后 diff vs master 恰好等于本 PR 原有 7 个文件(无夹带改动);全树无残留冲突标记;LIVE_FILES 两个条目都在。本次只动了冲突解决,未改任何 PR 原有逻辑。
    6. 是否破坏缓存:不涉及。
    7. 是否改了设计方向:不涉及(纯冲突消解,无架构/数据流转向)。
    8. 是否引入了新的配置:不涉及(LIVE_FILES 条目是 PR 自带的,非本次新增配置面)。

    收工判定:冲突已消解并 force-push(新 head ef645463596),PR 现为 mergeable,CI 正在新 head 上重跑(本次唯一一次完整 CI pass)。无需决策、无需后续动作——等 CI 绿即可由 owner 合并。

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