agentV2 点击停止后经常无法停止

Open
#7,665 3 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@c121914yu is already working on this.

Since Sep 9, 2026.

Assessment

This issue has not been assessed yet.

Description

feature

4.16.0

agentV2 点击停止后经常无法停止,F12 STOP如下:

Image

有时可以停止,F12 STOP如下

Image

分析

Redis 标志删除与 MongoDB 状态更新的竞态

时序问题:

工作流的 finally 块(dispatch/index.ts:388)先删除 Redis 停止标志
Stop API 的 waitForWorkflowComplete 检测到标志删除,立即读取 MongoDB 的 chatGenerateStatus
但 updateChatGenerateStatus 或 finalizeChatRound 是在工作流返回到 v2/chat/completions.ts:453 之后才调用的
此时 MongoDB 中的状态仍然是 generating (0),还没被更新为 done (1)
关键代码路径:

dispatch/index.ts finally 块:
  → delAgentRuntimeStopSign()  // 删除 Redis 标志
  
v2/chat/completions.ts:
  → await dispatchWorkFlow()   // 等待工作流返回
  → ... 后续处理 ...
  → updateChatGenerateStatus({ status: done })  // 更新 MongoDB 状态(在 Redis 删除之后!)
Stop API 在 Redis 标志删除后立即读取 MongoDB,但此时 chatGenerateStatus 还没更新,所以返回 completed: false。
工具执行阶段不检查停止信号

Agent Loop 的停止检查点:

LLM 流式输出时(stream.ts:44,128,202):每个 chunk 都检查 isAborted?.(),能快速响应
工具执行返回后(base.ts:651,679,685):只在工具完成后检查
循环迭代之间(base.ts:685):每轮 LLM 调用后检查
问题: 当 Agent 调用工具(如 HTTP 请求、沙盒执行、子应用、知识库搜索)时,工具执行期间不检查停止信号。如果工具执行时间 > 5 秒:
Stop API 超时返回 completed: false
工作流继续运行直到工具完成
用户看到"停止失败"

代码证据(base.ts:532):

const { response, ... } = await onRunTool({  // 这里 await 工具完成,期间不检查 stop
  call: tool,
  messages: cloneRequestMessages,
  assistantMessage: llmAssistantMessage
});

Stop API 超时返回 completed: false
工作流继续运行直到工具完成
用户看到"停止失败"
代码证据(base.ts:532):

const { response, ... } = await onRunTool({ // 这里 await 工具完成,期间不检查 stop
call: tool,
messages: cloneRequestMessages,
assistantMessage: llmAssistantMessage
});

Dominant language
TypeScript
Stars
29.9k
Forks
7.3k
Avg merge
1d 2h
Merged PRs (30d)
211

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from labring/FastGPT

All issues in labring/FastGPT

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.