[P1/高] ToolRegistry 工具超时后 Promise 未取消,导致资源泄漏 #11

Open
opened 2026-07-21 21:55:41 +08:00 by thzxx · 0 comments
Owner

问题类型

缺陷 / 高 / 工具系统

文件位置

electron/harness/tools/registry.ts

问题描述

ToolRegistry 使用 Promise.race([tool.execute(), timeout]) 实现超时控制。
超时触发后,timeout Promise resolve,但原 tool.execute() Promise 仍在后台运行,无法取消:

  1. 长时间运行的工具(如 WebFetch 抓取大文件、run_command 执行慢命令)持续消耗资源
  2. 工具可能继续修改文件系统状态,与后续操作冲突
  3. 内存与 CPU 泄漏

影响

  • 长时间运行后内存与 CPU 占用持续增长
  • 工具执行结果可能与超时后的操作产生竞态

建议修复

  1. 工具支持 AbortSignal:在 Tool.execute 签名中传入 AbortSignal
  2. 超时后触发 abort
async executeWithTimeout(tool, input, timeoutMs) {
  const controller = new AbortController();
  const timer = setTimeout(() => controller.abort(), timeoutMs);
  try {
    return await Promise.race([
      tool.execute(input, { signal: controller.signal }),
      new Promise((_, reject) =>
        controller.signal.addEventListener('abort', () =>
          reject(new Error('Tool timeout'))
        )
      ),
    ]);
  } finally {
    clearTimeout(timer);
  }
}
  1. 工具内部在耗时操作前检查 signal.aborted
## 问题类型 缺陷 / 高 / 工具系统 ## 文件位置 `electron/harness/tools/registry.ts` ## 问题描述 ToolRegistry 使用 `Promise.race([tool.execute(), timeout])` 实现超时控制。 超时触发后,timeout Promise resolve,但原 `tool.execute()` Promise 仍在后台运行,无法取消: 1. 长时间运行的工具(如 WebFetch 抓取大文件、run_command 执行慢命令)持续消耗资源 2. 工具可能继续修改文件系统状态,与后续操作冲突 3. 内存与 CPU 泄漏 ## 影响 - 长时间运行后内存与 CPU 占用持续增长 - 工具执行结果可能与超时后的操作产生竞态 ## 建议修复 1. **工具支持 AbortSignal**:在 `Tool.execute` 签名中传入 AbortSignal 2. **超时后触发 abort**: ```ts async executeWithTimeout(tool, input, timeoutMs) { const controller = new AbortController(); const timer = setTimeout(() => controller.abort(), timeoutMs); try { return await Promise.race([ tool.execute(input, { signal: controller.signal }), new Promise((_, reject) => controller.signal.addEventListener('abort', () => reject(new Error('Tool timeout')) ) ), ]); } finally { clearTimeout(timer); } } ``` 3. 工具内部在耗时操作前检查 `signal.aborted`
thzxx added the ??????? labels 2026-07-21 21:55:41 +08:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: MetonaTeam/metona-ai-desktop#11