Skip to content

[PR] fix: over-limit request bodies now return a readable 413 instead of ECONNRESET (#3128) - #3138

Closed
ranxianglei wants to merge 1 commit into
masterfrom
2026-10-14_fix-3128-receive-413
Closed

ranxianglei wants to merge 1 commit into
masterfrom
2026-10-14_fix-3128-receive-413

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

What this fixes (#3128)

A raw request body over 100 MiB destroyed the client socket BEFORE the structured 413 could be written (readBody in src/server.ts: req.destroy() then reject(BodyTooLargeError)). Clients met a bare ECONNRESET while bili's log recorded a 413 nobody ever received. The decode/forward stages were unaffected (socket still alive there) — only the receive stage.

Verified against the reporter's build: the v0.1.196 tag carries exactly this code (src/server.ts:3228-3229 at the tag).

Changes

  1. readBody stops consuming without destroying (src/server.ts): on overflow it removes its data listener and pauses the stream. The remaining body parks under TCP backpressure (bounded by stream HWM + kernel buffer, no unbounded heap growth), leaving the caller able to write the response.
  2. New respondRequestTooLarge(res, json) helper: writes the 413 with Connection: close. The header is required, not cosmetic: unread residual body bytes make the connection unusable for keep-alive reuse (the next request would parse over the leftovers). After the response, Node's destroySoon routes through the existing 建议:客户端侧连接请优雅关闭(close_notify+FIN),避免向下游客户端发出 RST #1982 post-response linger arm (graceful peer-close wait, bounded backstop) — no new timers, no new config.
  3. Every receive-stage 413 site uses it: the main request path (src/server/handle.ts) plus all 11 /__bili/* admin sites sharing readBody, including handleHostPluginDismiss whose catch-all previously answered over-limit bodies with a 400.
  4. Payload enrichment (additive): every request_too_large error now carries limitBytes + seenBytes in all three stages. The decode stage now reports observed decoded bytes (DecompressedTooLargeError gains an optional second ctor arg); the forward stage reports the exact rebuilt size. For the receive stage seenBytes is the count at the crossing, not the full length — the message says so.

Behavior changes (old → new)

aspect old new
receive-stage over-limit client experience socket destroyed pre-response ⇒ ECONNRESET/generic failure, 413 unreachable client READS a structured 413: {error:{type:"request_too_large",stage:"receive",message,limitBytes,seenBytes}} + Connection: close
connection after a receive-stage 413 immediate destroy (RST race) graceful close via the #1982 linger arm (peer-FIN wait, bounded backstop destroy)
413 payloads {type,stage,message} same + ADDITIVE limitBytes, seenBytes (all three stages)
host-plugin-dismiss catch-all on over-limit 400 "expected JSON body" 413 with the real message
warn log line 413: request body exceeds N bytes 413 [stage]: request body exceeds N bytes
caps/thresholds 100 MiB wire / 200 MiB decoded / 100 MiB outbound, 2 large-decode slots UNCHANGED

No config-surface changes. The configurable-budget half of #3128 is deliberately NOT in this PR — per the config-surface discipline it needs owner sign-off first (proposal already posted in the issue thread: top-level limits block, defaults = current constants).

Tests

  • tests/request-too-large-receive.test.ts (new, real HTTP): raw 101 MiB POST → the client reads a 413 with stage:"receive" + limitBytes/seenBytes + connection: close; upstream sees ZERO calls; a following small request proxies normally. The fixture teardown uses the sanctioned restartForceCloseOf seam because the 413'd connection can sit in the 建议:客户端侧连接请优雅关闭(close_notify+FIN),避免向下游客户端发出 RST #1982 linger arm until its backstop (the peer-FIN never arrives after an aborted upload).
  • tests/proxy-readbody.test.ts: rewritten to pin the NEW contract — rejection carries limit+seenBytes, the socket is NOT destroyed, the stream is paused, and late data/end cannot double-settle.
  • Targeted-local green: proxy-readbody, request-too-large-receive, request-body-budget, image-request-budget, content-encoding, decode-fail-passthrough, admin-origin, admin-probe; npm run typecheck clean. Full-suite verdict = CI on this head.

Closes #3128 (bug half; the config-budget half awaits the owner's decision in the issue thread)

…CONNRESET (#3128)

readBody destroyed the socket before the structured 413 could be written, so
clients met a bare connection reset on the receive stage while decode/forward
stages worked. Stop consuming without destroying (stream pause + TCP
backpressure bound the residual), answer receive-stage 413s with Connection:
close via respondRequestTooLarge() (residual unread bytes make the connection
unusable for keep-alive reuse; Node's destroySoon then feeds the #1982
post-response linger arm for a graceful close), and enrich every
request_too_large payload with limitBytes + seenBytes across all three stages.
Covers all /__bili/* admin sites sharing readBody, including the
handleHostPluginDismiss catch-all that previously answered 400.
@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-14_fix-3128-receive-413 (c4404d9)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.3572-pr.3138.3d46e3c

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr3138.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

PR #3138 独立审核完成(diff 逐行重读、机制逐条重验、验证重跑;未复用 #3128 讨论里的结论)。

方向关:#3128 要两件事——① 超限时客户端能可靠读到结构化 413;② 字节预算可配置。本 PR 只修 ①,② 明确留待你拍板(config-surface 纪律),方向正确、范围收敛。根因定位成立:readBody 超限先销毁 socket 再抛错,后面写的 413 落在死连接上;decode/forward 阶段抛错时 socket 还活着,所以只有接收阶段受影响——与报告人的对照实验吻合(raw 101MiB→ECONNRESET、gzip 解出 201MiB→正常 413)。修的是生产者(destroy 先于 reject 的次序),不是症状遮罩。

回归清点(diff 触碰路径的既有行为,逐项):

  • readBody 正常路径(<100MiB):逻辑未变,单测钉住 ✓
  • decode/forward 阶段 413:字段纯增量(limitBytes/seenBytes),路径未变 ✓(forward 的 limitBytes=MAX_REQUEST_BYTES 与 request-body-budget.ts:38 的出站判定核对一致)
  • 11 个 /__bili/* 管理站点:400 fallback 保留、各点 413 body 形状保留、仅加 Connection: close;全仓 12 个 readBody 调用点(admin 11 + 主路径 1)全部转换,无漏网点 ✓
  • 大解码槽位(并发 2)、上限值(100/200/100 MiB):零改动 ✓
  • 有意变更(PR 表格均已披露旧值→新值):ECONNRESET→可读 413;413 payload 增字段;receive 阶段 413 连接走 建议:客户端侧连接请优雅关闭(close_notify+FIN),避免向下游客户端发出 RST #1982 linger 机制(优雅关闭取代立即 destroy);warn 日志行加 [stage];host-plugin-dismiss 超限 400→413 ✓
  • 高危域(自动更新/dsh/lockfile/systemd/网络代理):与本 PR 无关,未触碰。

关键机制点(linger arm 声明核实成立):res 'finish' 时戳 lastResponseEndAt(src/server.ts:527);installPostResponseLinger 拦截 end/destroy——新路径 413 响应完成后 Node 的 destroySoon 同 tick 触发 end()+destroy(),两者都被拦下进入 linger:resume() 排空残余接收字节(去掉 RST 诱因)、等对端 FIN、5s backstop 强制关闭。有界、无 fd 泄漏、无新定时器/配置。旧路径裸 destroy() 不走 end(),直接绕过 linger arm——这正是客户端原来看到 RST race 的原因。

测试有效性:

  • proxy-readbody.test.ts(重写):断言有牙——seenBytes 精确值(MAX+16)、destroyed===false、isPaused()===true、迟到的 data/end 不二次结算;若旧 destroy 行为回归必红 ✓
  • request-too-large-receive.test.ts(新增):真实 HTTP 走 startServer + fake upstream,精确复现 issue 场景:客户端读到 413(stage=receive、limitBytes/seenBytes、connection: close)、upstream 0 次调用;第二例验证后续小请求正常代理。port 0 取端口、XDG 隔离、teardown 用 sanctioned restartForceCloseOf seam ✓
  • 覆盖边界:管理端超限 413 无专门 real-HTTP 用例(共享 readBody+respondRequestTooLarge,公共机制已被主路径 e2e 覆盖)——可接受,非阻塞。

验证证据:

结构化八问:

  1. 是什么问题:原始请求体超 100MiB 时,客户端在读到任何可读错误前就被断开(ECONNRESET);bili 日志记了 413,但客户端永远收不到。
  2. 什么原因导致的:readBody 超限处理里 req.destroy() 先于 reject——socket 先死,随后写入的 413 落在死连接上;仅接收阶段受影响(decode/forward 抛错时 socket 仍存活)。
  3. 影响范围与严重程度:任何单次 raw body >100MiB 的场景(大图片长会话等);失败信号不可行动(分不清"太大"还是"网络故障");特定条件下硬失败,上游不受影响。
  4. 怎么修复的:超限后停读但不杀连接(摘 data listener + pause,残余字节在 TCP 背压下驻留内核缓冲,有界),再写出带 Connection: close 的 413(存在未读残留字节就不能继续 keep-alive),响应后的关闭交给既有 建议:客户端侧连接请优雅关闭(close_notify+FIN),避免向下游客户端发出 RST #1982 linger 机制接管;三阶段统一补 limitBytes/seenBytes;11 个管理站点一并收敛。
  5. 是否有回归问题:无(见上清点;有意变更均已披露旧值→新值;上限/阈值/槽位零漂移)。
  6. 是否破坏缓存:不涉及——前缀缓存、压缩状态、重试/去重键均未触碰。
  7. 是否改了设计方向:否——在既有三段边界体系的拒绝路径内收敛,复用既有 linger 机制,无架构转向。
  8. 是否引入了新的配置:无(config-surface-guard 绿;limits 块提案等你拍板,见 [Bug/Enhancement] Codex 超过 100 MiB 的请求在 ACP 处理前断开:希望返回明确 413 并支持配置字节预算(#2606 后续) #3128 讨论)。

一个流程提示:PR 正文写的是 "Closes #3128"——合并会自动关掉整个 #3128,包括还在等拍板的 config-budget 那一半。若想留着 #3128 继续谈后半段,请合并前改成 "Partially addresses #3128"(或合并后批准方案时另开一单跟踪)。不影响代码质量判断。

结论:可合并(CI 全绿、合并树已本地验证)。按门禁规则,本 PR 涉及 wire 消息形态变化(413 payload/header),属人工合并类——请你自行合并:#3138

@ranxianglei

Copy link
Copy Markdown
Owner Author

(ai: zhipuai-lb/glm-5.3)

陈旧票清理(第三批动作①):目标 issue #3128 已关。为避免重复劳动关闭本 PR;如判断有误请直接 reopen 并留言。

@ranxianglei ranxianglei changed the title fix: over-limit request bodies now return a readable 413 instead of ECONNRESET (#3128) [PR] fix: over-limit request bodies now return a readable 413 instead of ECONNRESET (#3128) Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug/Enhancement] Codex 超过 100 MiB 的请求在 ACP 处理前断开:希望返回明确 413 并支持配置字节预算(#2606 后续)

1 participant