refactor: extract Close fork cleanup to closeExt in loop_ext.go

Move done channel close, stats flush, and sessions close into
closeExt(). Close() now calls closeExt() + MCP/registry close.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
dj-oyu 2026-03-15 18:39:12 +09:00
parent 710b703f35
commit 7512b6e8f3
2 changed files with 24 additions and 21 deletions

View file

@ -520,15 +520,7 @@ func (al *AgentLoop) Stop() {
// and dirty session data). Should be called during graceful shutdown.
func (al *AgentLoop) Close() {
select {
case <-al.done:
// already closed
default:
close(al.done)
}
al.closeExt()
mcpManager := al.mcp.takeManager()
if mcpManager != nil {
@ -540,18 +532,7 @@ func (al *AgentLoop) Close() {
}
}
if al.stats != nil {
al.stats.Close()
}
registry := al.GetRegistry()
for _, agentID := range registry.ListAgentIDs() {
if agent, ok := registry.GetAgent(agentID); ok {
agent.Sessions.Close()
}
}
registry.Close()
al.GetRegistry().Close()
}
func (al *AgentLoop) RegisterTool(tool tools.Tool) {

View file

@ -75,6 +75,28 @@ func (al *AgentLoop) initLoopExt(cfg *config.Config, registry *AgentRegistry, en
go al.gcLoop()
}
// closeExt releases fork-specific resources: done channel, stats tracker,
// and session stores for all agents.
func (al *AgentLoop) closeExt() {
select {
case <-al.done:
// already closed
default:
close(al.done)
}
if al.stats != nil {
al.stats.Close()
}
registry := al.GetRegistry()
for _, agentID := range registry.ListAgentIDs() {
if agent, ok := registry.GetAgent(agentID); ok {
agent.Sessions.Close()
}
}
}
// SetConfigSaver registers a callback to persist config changes.
func (al *AgentLoop) SetConfigSaver(fn func(*config.Config) error) {
al.saveConfig = fn