Skip to content

fix(chat): stop send-time OOM crash from references reactive infinite loop#1416

Merged
Germey merged 1 commit into
mainfrom
fix/chat-references-loop
Jul 23, 2026
Merged

fix(chat): stop send-time OOM crash from references reactive infinite loop#1416
Germey merged 1 commit into
mainfrom
fix/chat-references-loop

Conversation

@acedatacloud-dev

Copy link
Copy Markdown
Member

P0 — 发消息卡死/OOM 的真正原因

发任何一条消息(哪怕只是 `hi`,任何账号)都会冻结并 OOM 崩溃标签页。Console 是铁证:一次发送打印了 107,725 条 `references changed` —— Composer ↔ Conversation 之间的响应式无限循环:

  1. 发送 → `Conversation.vue` `this.references = []`(新数组)
  2. Composer `references` watcher → `this.fileList = []`(无条件,新数组)
  3. `fileList` 变 → `refs` computed → 新 `[]`
  4. `refs` watcher → `$emit('update:references', [])` —— PR feat(chat): preserve browser resource metadata #1398 删掉了它原有的 `if (val.length > 0)` 守卫,导致空值也回传
  5. 父组件 `@update:references="references = $event"` → `references = []` → 回到第 2 步 →

第 4 步守卫的移除就是"昨天还行今天崩"的真正回归点(#1398 于 7/23 01:33 凌晨合并),且与账号轻重无关,人人发消息都崩

修复

在 Composer 的 `references` watcher 上给回边加守卫 —— 仅当有内容可清 且没有上传进行中 时才重置 `fileList`:

```js
if (val.length === 0 && this.fileList.length > 0 && !this.uploading) {
this.fileList = [];
}
```

  • `fileList.length > 0`:断开空数组 ping-pong(核心)。
  • `!uploading`:上传进行中时 `fileList` 非空但 `refs` 仍是 `[]`(还没拿到 `response.file_url`),父组件会短暂持有 `[]` —— 若此时清空会误删正在上传的文件。发送按钮本就 gated on `!uploading`,所以发送后的合法清空一定通过此守卫。

同时删掉两处放大损害的 debug 日志(`references changed`、`References:`)。

回归测试 `Composer.loop.test.ts`(3 个,每个都验证过"改回 bug 就挂")

  • (a) 显式接上父组件回边,断言 emit 次数有界、必须收敛(改回无守卫 → 挂)
  • (b) 已完成上传在空重置时正常清空
  • (c) 进行中的上传不被误清(改回无 `!uploading` → 挂)

验证

  • `vitest run src/components/chat/ src/store/chat/` → 93 passed
  • `vue-tsc --noEmit` + `eslint` 干净

🤖 对抗评审

Codex (`gpt-5.5`, `codex exec --sandbox read-only`) 复核。发现一个真实 RISK(已采纳修复):初版守卫 `if(val.length===0 && fileList.length>0)` 会在文件上传进行中误删附件 —— 因为此时 `fileList` 非空但 `refs` 为空,父组件短暂持有 `[]` 触发清空。据此加了 `!uploading` 守卫,并补了上传保护回归测试 (c)。Codex 同时确认:单独恢复 `refs` watcher 的 `if(val.length>0)` 会破坏"用户删掉最后一个附件、父组件需被告知"的合法场景 —— 故不采用该方案。收敛后无残留 RISK。

#1415 的关系

本 PR 是发消息崩溃的真正修复。先前 #1415(持久化只存 summary)修的是另一个真实但独立的性能问题(重账号持久化全量历史,序列化 158ms),不是本次崩溃主因,保留无害。

🤖 Generated with Claude Code

… loop

Sending any chat message (even a bare "hi", any account) froze and OOM-crashed
the tab. Console proved it: one send logged `references changed` 107,725 times —
an infinite reactive loop between Composer and Conversation:

1. send → Conversation `this.references = []` (fresh array)
2. Composer `references` watcher → `this.fileList = []` (unconditional, fresh array)
3. fileList change → `refs` computed → new `[]`
4. `refs` watcher → `$emit('update:references', [])` — PR #1398 removed its
   prior `if (val.length > 0)` guard, so it now echoes empty values
5. parent `@update:references="references = $event"` → `references = []` → step 2 → ∞

Fix: guard the back-edge in the Composer `references` watcher — only reset
`fileList` when there is something to clear AND nothing is mid-upload:
`if (val.length === 0 && this.fileList.length > 0 && !this.uploading)`.
The `!uploading` clause prevents clobbering an in-flight upload (fileList is
non-empty but `refs` is still [] until the response arrives, so the parent
momentarily holds []). Send is gated on `!uploading`, so the post-send reset
we DO want to clear always passes. Also removed two stray debug logs
(`references changed`, `References:`) that amplified the loop's cost.

Regression tests (Composer.loop.test.ts): (a) parent back-edge wired — emit
count stays bounded, must settle; (b) completed upload clears on empty reset;
(c) in-flight upload is preserved. Each fails if its guard clause is reverted.

Note: this is the actual send-crash cause. The earlier persist-summaries change
(#1415) fixed a separate real perf issue (heavy persisted history) but not this.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
nexior 889c271 Commit Preview URL

Branch Preview URL
Jul 23 2026, 07:15 AM

@Germey
Germey merged commit b61b10a into main Jul 23, 2026
3 of 4 checks passed
@Germey
Germey deleted the fix/chat-references-loop branch July 23, 2026 07:17
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.

2 participants