WeKnora/internal/agent/tools/goroutine.go

51 lines
1.8 KiB
Go
Raw Normal View History

fix(agent): 工具自起 goroutine 的 panic 自行兜底,MCP 预热不再带走进程 (#3846) recover 只对调用它的那个 goroutine 生效。executeRecovered(registry.go)覆盖的是 tool.Execute 这次调用本身,act.go 的 errgroup 回调同理:工具自己起的 goroutine 上 发生 panic,两处都接不到,整个进程退出,正在进行的对话、上传、同步全部中断。 修复站点: - mcp_exposure.go 的 catalog 预热 goroutine 与最多 8 个 worker 各装一道屏障;worker 内按服务再兜一层,panic 时沿用文件里既有的 store/status 机制把该服务标记为 "error"(与加载报错同一个状态)、清掉 partial snapshot,close(preloadDone) 照常 执行,prepareMCPToolsWithMode 的 select 仍能返回。 - search_knowledge.go 检索扇出的三处 goroutine(embedding 分组、合并 KB 检索、单 KB 检索):panic 记日志后按既有的 fail() 机制把对应知识库报成失败,工具调用降级为 失败结果而不是进程退出。 - think.go 的 stream stall 看门狗改用同一屏障(stallTimeout 非正时 NewTicker 会 panic)。 新增 internal/agent/tools/goroutine.go: - GoRecovered(ctx, fn):起 goroutine 并兜住 panic,供工具内部统一使用; - RecoverGoroutine(ctx, label, onPanic):defer 形式,onPanic 在同一 goroutine 上记录 失败单元,panic 值不外传。 registry.go 的 executeRecovered 注释补上"recover 只覆盖本 goroutine,工具自起的 goroutine 必须自己兜底"。 回归测试: - TestMCPPreloadWorkerPanicIsContained:loader panic 后进程存活、PrepareMCPTools 正常 返回、该服务 status 为 error 且不保留缓存工具; - TestMCPPreloadFailureMarkingKeepsReadySnapshots:协调者屏障只标记未完成的加载, ready 快照不动; - TestToolChildGoroutinePanicDegradesToFailedResult:走真实 executeToolCallsParallel, 子 goroutine panic 降级为失败结果; - TestGoRecoveredContainsPanicAndRunsDefers / TestRecoverGoroutineReportsPanicToCaller。 去掉屏障后上述用例的测试进程 exit=1,装回后全部通过。 Fixes #3837 Co-authored-by: linus-liu-web <11640009+linus-liu-web@users.noreply.github.com>
2026-10-07 17:24:36 +00:00
package tools
import (
"context"
"runtime/debug"
"github.com/Tencent/WeKnora/internal/logger"
)
// GoRecovered runs fn on a new goroutine and contains a panic raised there.
//
// recover only covers the goroutine that calls it, so the barrier in
// executeRecovered (registry.go) protects the call to tool.Execute alone: a
// goroutine a tool starts for itself — an MCP client, a sandbox, a third-party
// SDK, a retrieval adapter — must install its own barrier, or one panic escapes
// every recover site and takes the whole server (and every conversation,
// upload and sync in flight) down with it.
//
// Deferred calls inside fn still run while a panic unwinds, so wait groups and
// cleanup are released. A caller that must also degrade the failed work into an
// error result uses RecoverGoroutine directly and records it in onPanic.
func GoRecovered(ctx context.Context, fn func()) {
go func() {
defer RecoverGoroutine(ctx, "", nil)
fn()
}()
}
// RecoverGoroutine is deferred by a goroutine that must not let a panic escape
// (either directly or through GoRecovered). It logs the panic with its stack,
// then calls onPanic — when set — on that same goroutine so the caller can mark
// the failed unit of work: one MCP service, one knowledge base, one retrieval
// call. label names that work in the log; an empty one leaves it unnamed.
//
// onPanic deliberately receives no panic value: a recovered value carries
// internals (paths, addresses) that must not reach the model or the user.
func RecoverGoroutine(ctx context.Context, label string, onPanic func()) {
r := recover()
if r == nil {
return
}
where := "[ToolGoroutine]"
if label != "" {
where += " " + label
}
logger.Errorf(ctx, "%s panicked: %v\n%s", where, r, debug.Stack())
if onPanic != nil {
onPanic()
}
}