fix(agent): ensure MCP cleanup on all Run() exit paths

- Add defer in Run() to guarantee MCP connection cleanup
- Handles both normal termination and context cancellation
- Prevents resource leaks when Run() exits via ctx.Done()
- MCP Manager.Close() is idempotent, safe to call from both defer and Stop()

This fixes GitHub Copilot feedback that MCP cleanup only happened in
Stop() but Run() could return on ctx.Done() without cleanup, causing
subprocess/session leaks on normal cancellation shutdown.
This commit is contained in:
yuchou87 2026-02-16 19:56:00 +08:00
parent 8f89356b64
commit 4f4a559583

View file

@ -192,6 +192,18 @@ func (al *AgentLoop) Run(ctx context.Context) error {
}
})
// Ensure MCP connections are cleaned up on all exit paths
defer func() {
if al.mcpManager != nil {
if err := al.mcpManager.Close(); err != nil {
logger.ErrorCF("agent", "Failed to close MCP manager",
map[string]interface{}{
"error": err.Error(),
})
}
}
}()
for al.running.Load() {
select {
case <-ctx.Done():