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:
parent
710b703f35
commit
7512b6e8f3
2 changed files with 24 additions and 21 deletions
|
|
@ -520,15 +520,7 @@ func (al *AgentLoop) Stop() {
|
||||||
// and dirty session data). Should be called during graceful shutdown.
|
// and dirty session data). Should be called during graceful shutdown.
|
||||||
|
|
||||||
func (al *AgentLoop) Close() {
|
func (al *AgentLoop) Close() {
|
||||||
select {
|
al.closeExt()
|
||||||
case <-al.done:
|
|
||||||
|
|
||||||
// already closed
|
|
||||||
|
|
||||||
default:
|
|
||||||
|
|
||||||
close(al.done)
|
|
||||||
}
|
|
||||||
|
|
||||||
mcpManager := al.mcp.takeManager()
|
mcpManager := al.mcp.takeManager()
|
||||||
if mcpManager != nil {
|
if mcpManager != nil {
|
||||||
|
|
@ -540,18 +532,7 @@ func (al *AgentLoop) Close() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if al.stats != nil {
|
al.GetRegistry().Close()
|
||||||
al.stats.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
registry := al.GetRegistry()
|
|
||||||
for _, agentID := range registry.ListAgentIDs() {
|
|
||||||
if agent, ok := registry.GetAgent(agentID); ok {
|
|
||||||
agent.Sessions.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
registry.Close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (al *AgentLoop) RegisterTool(tool tools.Tool) {
|
func (al *AgentLoop) RegisterTool(tool tools.Tool) {
|
||||||
|
|
|
||||||
|
|
@ -75,6 +75,28 @@ func (al *AgentLoop) initLoopExt(cfg *config.Config, registry *AgentRegistry, en
|
||||||
go al.gcLoop()
|
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.
|
// SetConfigSaver registers a callback to persist config changes.
|
||||||
func (al *AgentLoop) SetConfigSaver(fn func(*config.Config) error) {
|
func (al *AgentLoop) SetConfigSaver(fn func(*config.Config) error) {
|
||||||
al.saveConfig = fn
|
al.saveConfig = fn
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue