fix: 优雅停机停等任务 goroutine,根治 TempDir 清理竞态
新增 Server.Close / Engine.StopAll:取消 ctx 后依次等 side-flusher、 后台循环、各任务 planner/worker、归档 worker 退出;测试用 defer s.Close() 在清理前排空 goroutine,遂把 tempDir 兜底改回 t.TempDir() 并删除 helper。
This commit is contained in:
1 parent
f8454d310b
commit
98515a3974
10 files changed
+75
-6
No files matched your search
+43
-6
@@ -42,6 +42,12 @@ type Server struct {
|
||||
m *Manager
|
||||
engine *Engine
|
||||
ctx context.Context
|
||||
// cancel tears down ctx; Close() calls it to stop every background goroutine
|
||||
// started by New (scheduler, notifier, evidence GC, task-archive worker and all
|
||||
// per-task planner/worker/deadline loops). bgWG tracks the server-level loops so
|
||||
// Close can wait for them to return before the data dir is removed.
|
||||
cancel context.CancelFunc
|
||||
bgWG sync.WaitGroup
|
||||
|
||||
skillDir string // root directory for skill subdirectories
|
||||
jwtKey []byte // HS256 signing key loaded from / generated into dataDir/jwt.key
|
||||
@@ -145,7 +151,11 @@ func New(ctx context.Context, m *Manager, skillDir string, dataDir string, keyDi
|
||||
if err != nil {
|
||||
log.Fatalf("[auth] JWT key: %v", err)
|
||||
}
|
||||
s := &Server{m: m, engine: NewEngine(m), ctx: ctx, skillDir: skillDir, jwtKey: key, chatBusy: map[string]bool{},
|
||||
// Own a cancelable child of the caller's ctx so Close() can stop every
|
||||
// background goroutine below even when the caller passed a context that never
|
||||
// cancels (e.g. context.Background() in tests).
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
s := &Server{m: m, engine: NewEngine(m), ctx: ctx, cancel: cancel, skillDir: skillDir, jwtKey: key, chatBusy: map[string]bool{},
|
||||
chatCancel: map[string]context.CancelCauseFunc{}, triggerQ: map[string][]triggeredRun{},
|
||||
triggerActive: map[string]int{}, triggerCfg: map[string]triggerBehavior{},
|
||||
profChatAgents: map[int64]*agent.ChatAgent{},
|
||||
@@ -225,12 +235,17 @@ func New(ctx context.Context, m *Manager, skillDir string, dataDir string, keyDi
|
||||
if err := s.seedFindingRetester(); err != nil {
|
||||
log.Printf("[retester] seed: %v", err)
|
||||
}
|
||||
go s.evidenceStore().RunGC(s.ctx)
|
||||
s.seedPythonInterpreter() // 自定义脚本工具:开机检测 python 解释器入库(仅空时)
|
||||
go newScheduler(s).Run(s.ctx) // P3 触发器调度(定时/finding/目标事件),仅自定义 agent
|
||||
s.bgWG.Add(1)
|
||||
go func() { defer s.bgWG.Done(); s.evidenceStore().RunGC(s.ctx) }()
|
||||
s.seedPythonInterpreter() // 自定义脚本工具:开机检测 python 解释器入库(仅空时)
|
||||
sc := newScheduler(s)
|
||||
s.bgWG.Add(1)
|
||||
go func() { defer s.bgWG.Done(); sc.Run(s.ctx) }() // P3 触发器调度(定时/finding/目标事件),仅自定义 agent
|
||||
// 漏洞 IM 推送投递引擎。与 Scheduler 并列但独立:推送的实时性要求(3s)
|
||||
// 与触发器的业务节奏不同,且两者失败互不牵连——推送卡住不该影响 agent 触发。
|
||||
go newNotifier(s).Run(s.ctx)
|
||||
nt := newNotifier(s)
|
||||
s.bgWG.Add(1)
|
||||
go func() { defer s.bgWG.Done(); nt.Run(s.ctx) }()
|
||||
// Fill the tool cache for any enabled MCP that has none yet (notably the
|
||||
// seeded browser MCP on first run). Async so it never blocks startup.
|
||||
go s.discoverEmptyMCPsOnStartup()
|
||||
@@ -253,12 +268,34 @@ func New(ctx context.Context, m *Manager, skillDir string, dataDir string, keyDi
|
||||
log.Printf("[engine] no LLM provider configured — engine idle until set via /api/llm or env")
|
||||
}
|
||||
s.restoreTaskRuntimes()
|
||||
go s.reconcileConcurrency()
|
||||
s.bgWG.Add(1)
|
||||
go func() { defer s.bgWG.Done(); s.reconcileConcurrency() }()
|
||||
s.startTaskArchiveWorker()
|
||||
s.wireInterceptReviewer() // LLM 兜底审批:未命中拦截规则的命令交给模型判定
|
||||
return s
|
||||
}
|
||||
|
||||
// Close stops every background goroutine started by New and waits for them to
|
||||
// return, so nothing writes into the data dir afterwards. It cancels the server
|
||||
// context (stopping the scheduler, notifier, evidence GC, task-archive worker and
|
||||
// every task's planner/worker/deadline loops), then waits: first the server-level
|
||||
// loops, then the per-task runtimes, then the archive worker. Call it before
|
||||
// Manager.Close() (which closes the DB) and before the data dir is removed — in
|
||||
// tests, before t.TempDir() cleanup runs, which otherwise races a late task-file or
|
||||
// archive write ("TempDir RemoveAll cleanup: directory not empty"). Safe to call
|
||||
// once; the engine waits are idempotent for already-exited runtimes.
|
||||
func (s *Server) Close() {
|
||||
if s.cancel != nil {
|
||||
s.cancel()
|
||||
}
|
||||
if s.side != nil {
|
||||
<-s.side.done // side-question snapshot flusher (final flush on ctx.Done)
|
||||
}
|
||||
s.bgWG.Wait() // scheduler / notifier / evidence GC / startup reconcile
|
||||
s.engine.StopAll() // per-task planner / worker / deadline loops
|
||||
s.archiveWG.Wait() // cold task-archive worker
|
||||
}
|
||||
|
||||
// Restored deadline and worker loops must inherit the same context as new tasks,
|
||||
// including the side-question checkpoint publisher installed during startup.
|
||||
func (s *Server) restoreTaskRuntimes() {
|
||||
|
||||
Reference in new issue
Block a user