Skip to content

[PR] fix(session): discharge raw lastMessages AST on request release to prevent OOM (#3050) - #4967

Closed
fathonix wants to merge 9 commits into
ranxianglei:masterfrom
fathonix:fix/discharge-last-messages-on-release
Closed

fathonix wants to merge 9 commits into
ranxianglei:masterfrom
fathonix:fix/discharge-last-messages-on-release

Conversation

@fathonix

Copy link
Copy Markdown
Contributor

Problem

Under long-running client sessions with large raw transcripts (e.g. 5,000+ messages / ~11 MiB incoming request payload), Billion-Context can crash with V8 heap exhaustion:

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

Root Cause

While #3050 R3 ②a (foldResidentTail) intended for idle sessions to keep only the bounded folded view resident in memory (lastMessagesFolded), this pruning was only called during asynchronous disk persistence (saveNow / debounceMs timer / flushSync).

When requests arrive in continuous succession or between persistence windows, session.lastMessages permanently pins the incoming multi-megabyte unpruned AST object graph in RAM across turns. Multiple subsequent turns cause garbage collection thrashing near the 2048 MB heap ceiling.

Solution

  1. Discharge on release: In releaseInFlight(session) (src/session.ts), when session.inFlight === 0, immediately fold session.lastMessages to its bounded view via boundedFoldedSnapshot(session).
  2. Reclaim allocations: Trigger global.gc() if exposed (--expose-gc) when releasing in-flight markers to collect request allocations promptly.
  3. Export snapshot helper: Export boundedFoldedSnapshot(session) from src/persist.ts.
  4. Unit test: Added test coverage in tests/bounded-storage.test.ts verifying that completing an in-flight request folds lastMessages and marks lastMessagesFolded = true.

Verification

  • Local test suite passed (node --import tsx --test tests/bounded-storage.test.ts -> 12/12 pass).
  • Live verification in production container: resident heap dropped from ~1.9 GB down to ~43.5 MiB, and breakdown.lastMessages dropped from 35.6 MiB to 217 KiB.

@ranxianglei ranxianglei changed the title fix(session): discharge raw lastMessages AST on request release to prevent OOM (#3050) [PR] fix(session): discharge raw lastMessages AST on request release to prevent OOM (#3050) Oct 27, 2026
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

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

查重:不重复。按失败签名 + 机制检索 open/closed issue 与 PR,同族但机制不同:#4765(bounded session loading / lazy fields,懒加载而非 release 折叠)、#4888(retire superseded msg_id generations,存储去重)、#4761(web blocks-pane preview fallback,UI 回查)、#3024(fork adoption 409,fork 采纳)。本 PR 是 #3050(R3 ②a foldResidentTail)的跟进,不构成重复。

标签:bug + p1 + scope/storage(沿用既有 pr)。理由:长会话堆耗尽崩溃属可用性缺陷(p1);改动落在会话驻留内存 + 统一存储持久化语义(scope/storage)。

判定:拦下,不建议按当前形态合并。

一句话:这个修法把一个「顺序敏感」的操作当成了「顺序无关」的操作。#3050 R3 ②a 的折叠之所以安全,前提是「一次 save 已经把原始尾部摄入统一存储之后」才折叠;本 PR 把折叠挪到 releaseInFlight(请求完成时),而它发生在每轮去抖 save 之前,于是 lastMessagesFolded 这个位在原始尾部被摄入之前就被翻成 true,直接掐断了摄入路径 → 数据丢失风险。PR 描述把它说成「只是把同一份裁剪提前调用」,但这份裁剪不是幂等可任意挪位的——它的安全由「先摄入、后折叠」的顺序定义。

结构化八问

  1. 是什么问题:长客户端会话(5000+ 消息、~11MiB 请求体)下,session.lastMessages 在轮次之间持续钉住多 MiB 的未裁剪 AST,V8 堆逼近 2048MB 上限触发 mark-compact OOM 崩溃。
  2. 什么原因导致的:「idle 会话只驻留 bounded folded view」这条优化(Memory plane (R3): stop keeping compressed originals, CCR and snapshots fully in RAM now that the unified store has them — measure first, then lazy-load #3050 R3 ②a)只在高层 writeNow/flushSync 里经 foldResidentTail 生效(src/persist.ts:2611/2619/2699/2717);而常规每轮持久化走去抖路径 scheduleSave(src/persist.ts:2573-2583,低层 store.writeNow,不调用 foldResidentTail)。稳态下去抖 save 从不折叠,snapshotMessages(src/session.ts:1370)每轮又把 lastMessages 重置为全量 raw(folded=false),raw AST 跨轮常驻。作者诊断方向对,错在修法挂载点。
  3. 影响范围与严重程度:受影响面 = 走统一存储(shadow/full)且出现「单轮耗时 < debounceMs(默认 500ms,src/knobs.ts:369-371)」的长会话。严重度 = 数据丢失(p1):某轮 net-new 内容若在其自身去抖 save 触发前就被折叠,后续 save 见 folded=true 便不再摄入原始尾部,该内容进不了 index.db;只有更慢的下一轮重发该历史时才补上,崩溃/重启恰好落在该窗口即永久丢。连续慢轮大多自愈,故非必现,但是真实持久性回归。
  4. 怎么修复的(本 PR 的修法 + 我的判断):本 PR 在 releaseInFlight 里 inFlight===0 时无条件 boundedFoldedSnapshot 折叠并置 folded=true,外加 global.gc()。判断:挂载点错误。 guardedBuild(src/persist.ts:2393)const tail = session.lastMessagesFolded !== true ? session.lastMessages : undefined; —— 原始尾部入统一库只在该位为 false 时发生;其注释(2383-2392)明写 “the raw rows stay interned from the saves that saw them”。把翻位提前到任何可能尚未摄入的 save 之前,就违反了这条不变量。
  5. 是否有回归问题:有,核心回归即上述数据丢失。命中高危模式「修 A(OOM)坏 B(持久化语义静默漂移——落盘内容变少且无告警)」。逐项过既有行为:envelope 的 messages 字段不受影响(buildRecord 恒用 boundedFoldedSnapshot,src/persist.ts:2857);受影响的恰是统一存储的 raw 尾部摄入。
  6. 是否破坏缓存:不涉及 prefix-cache 命中本身;影响的是持久化/重启恢复的完整性(恢复出的历史比应有更少)。
  7. 是否改了设计方向:改了持久化时序契约——从「折叠严格晚于摄入」变为「折叠可能与摄入竞态」。这是 storage 语义的方向性变更,属 §7.4 human-gated 面,不宜由单个外部 PR 单方定调。
  8. 是否引入新配置:无新 env/config(--expose-gc 是 Node 启动旗标,非本项目配置面)。

其它发现(次要,合并前须一并处理)

  • global.gc() 过度且有害:每次请求完成都同步全量 GC(仅 --expose-gc 时),阻塞事件循环、放大尾延迟;默认部署(不开 expose-gc)下是死代码;而且并不需要——折叠已释放引用,V8 会自然回收。删掉。(globalThis as unknown as {gc?...}) 双重断言也啰嗦。
  • 测试没有牙:新增用例(tests/bounded-storage.test.ts)只断言 lastMessagesFolded===true && length>0,测的是「位翻了」这个症状,没测真正的安全属性(折叠前原始尾部已持久化 / 重启 round-trip 能否恢复全量历史)。它在数据丢失回归存在时照样绿——§7.2/④ 意义上的假测试。须补 round-trip 持久化断言 + 负例(快轮后立即 reload 不丢内容)。
  • DRY:内联折叠与 foldResidentTail 逻辑重复,应复用或至少同条件门控。

建议的正确修法(疏,非堵):真正缺口是「去抖 save 路径从不折叠」。应让折叠发生在成功 save 摄入原始尾部之后,而非挪进 releaseInFlight:

  • 首选(C):在去抖 save 完成回调里补调 foldResidentTail(writerOff 微任务路径同理)。idle 会话每轮 save 后即折叠(关掉观察到的缺口),且天然保持「先摄入后折叠」;foldResidentTail 已有的 inFlight>0 return 守卫保证新请求进来时不误折。快轮也安全——它自身去抖 save(start+500ms)仍先摄入再折叠。
  • 备选(A):若坚持 releaseInFlight 的即时性,须新增「自上次 snapshotMessages 以来原始尾部已被摄入」的标记(guardedBuild 摄入非空 raw 尾部时置位、snapshotMessages 清位),releaseInFlight 仅在该标记为真时折叠。绝不能在摄入前翻 lastMessagesFolded。
  • 同时删 gc()、补强测试。

我不擅自替你重构这块:它落在 storage 语义(human-gated),且正确形态需在 C/A 间拍板。你确认方向后我可以直接按选定方案实现并补 round-trip 回归钉。

合并判定:拦下。 目标(OOM)成立、方向(idle 折叠)正确,但当前实现违反 #3050 R3 ②a 的「先摄入后折叠」不变量,引入持久化数据丢失回归,且现有测试无法兜底;请按 C/A 之一重做。

@ranxianglei ranxianglei added bug Something isn't working p1 scope/storage labels Oct 27, 2026
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)
Synced origin/master into this branch (two clean merges, no conflicts; second picked up the 0.1.208 version-sync lines only), local typecheck + build green on merged head, pushed as 419f91e — PR is now up to date with master and CI is re-running on the new head.

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

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

CI 红诊断 + 处置完成(本车道 attempt 2/2,末轮)

查重: 不重复——本轮三片红与 master 自身红同签名(#5144/#5140 家族,#4599 floors 486–487 已有在档记录),与任何在途修复项不重复;处置走既有「收敛到常驻分支」路径。

诊断: 三个分片的唯一失败全部是同一 subtest HTTP a cold-restored non-retained parent serves its intact raw tail immediately (#5140)(tests/plugin-fork.test.ts:1473,409 !== 200)——与 master 自身的间歇红逐字一致(同一 sha 在 master 上红绿 run 并存,见 #4599 floor 486 的逐字核对)。本 PR 的 diff(releaseInFlight 折叠 + boundedFoldedSnapshot 导出 + 一个测试)与该路径零交集 → 继承性失败,非本 PR 引入。

处置(按 CI 协调政策): 已把 upstream/master@a8c907dd5 + upstream/ci-fixes@cf99d935f 合入 PR 分支,零冲突(分支同步点之后 master 未触碰 persist/session/plugin-fork 文件),本地 typecheck + build 绿,推送 23420cb,CI 重跑中(快车道已绿)。注意:ci-fixes 目前不含该 flake 本身的修复(它是间歇性负载时序竞态);若 23420cb 上同签名再红,属哨兵/master 侧事项,本 PR 不再自行排查。

另一件与本次 CI 无关的阻塞: 本 PR 的审核结论仍是拦下——其折叠时机违反 #3050 R3②a「先摄入后折叠」不变量,加载高时该轮新增内容不会进统一存储(数据丢失窗口,p1 类)。修法方向需 owner 拍板,已单独立项:#5190(decision request,选项 A vs C,推荐 C)。拍板后由我在本分支内闭环重构。

结构化八问

  1. 是什么问题: 本轮 CI 三片红不是本 PR 的代码问题——master 自身在同一 subtest 上间歇红([PR] fix: recover non-retained parent fork snapshot from intact unified raw tail after cold restart (#5140) #5144 cold-raw-tail fork 的 [Bug][fork] Cold restore cannot use an intact unified raw tail for a non-retained parent (#4266 item 4) #5140 回归测试),属继承性失败。
  2. 什么原因导致的: [PR] fix: recover non-retained parent fork snapshot from intact unified raw tail after cold restart (#5140) #5144(10-29 合入)的冷恢复路径带负载时序竞态(期望 200 得 409),master 同 sha 红绿 run 并存;本 PR 未触碰该路径。
  3. 影响范围与严重程度: 仅 CI 验证面(凡分片含该文件的 PR 都可能间歇红);生产零影响;严重度 = CI 噪音,因阻塞验证按协调政策以 p1 对待。
  4. 怎么修复的: 按政策收敛到常驻分支——merge master + ci-fixes 进 PR 分支(sha=23420cb8b),本地 typecheck/build 绿,CI 重跑中;未改本 PR 任何逻辑行。
  5. 是否有回归问题: 无——纯合并收敛;已核同步点→当前 master 之间对 src/persist.ts、src/session.ts、tests/plugin-fork.test.ts 零提交;PR 的 3-file diff 合并后原样保留(逐行验证过)。
  6. 是否破坏缓存: 不涉及。
  7. 是否改了设计方向: 本次合并无。但 PR 本身尚有一个待拍板的设计决策(折叠时机 = 数据丢失风险),见 decision request: #4967 OOM fix folds session history before it is saved — pick the rework option (A vs C) #5190——与本次 CI 事故独立。
  8. 是否引入了新的配置: 无。

判定: 本车道收工(继承红已按政策处置,attempt 2/2 末轮)。PR 自身阻塞项 = #5190 待 owner 拍板,拍板后在本分支闭环重构。

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

Labels

bug Something isn't working p1 scope/storage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants