From 94844b71395d38ea622aae080861540be3a7755c Mon Sep 17 00:00:00 2001 From: shinchan-zhai Date: Mon, 11 May 2026 16:37:30 +0800 Subject: [PATCH] fix(agent): guard async maintenance goroutine and add timeout to diagnosis ctx - Add stopCh check in runPostResponseMaintenanceAsync to respect agent shutdown, preventing goroutine leak on Agent.Stop() - Replace bare context.Background() in handleTraderDiagnosisSkill with a 30s timeout context for proper deadline propagation Co-Authored-By: Claude Opus 4.6 --- agent/planner_runtime.go | 6 ++++++ agent/skill_execution_handlers.go | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/agent/planner_runtime.go b/agent/planner_runtime.go index 36e34377..bc4dfbcb 100644 --- a/agent/planner_runtime.go +++ b/agent/planner_runtime.go @@ -2628,6 +2628,12 @@ func (a *Agent) runPostResponseMaintenanceAsync(userID int64) { }() ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) defer cancel() + // Respect agent shutdown: abort early if stopCh is closed. + select { + case <-a.stopCh: + return + default: + } a.maybeUpdateTaskStateIncrementally(ctx, userID) a.maybeCompressHistory(ctx, userID) }() diff --git a/agent/skill_execution_handlers.go b/agent/skill_execution_handlers.go index 44357506..f1fdda68 100644 --- a/agent/skill_execution_handlers.go +++ b/agent/skill_execution_handlers.go @@ -7,6 +7,7 @@ import ( "regexp" "strconv" "strings" + "time" "nofx/mcp" "nofx/store" @@ -2757,7 +2758,9 @@ func (a *Agent) handleTraderDiagnosisSkill(storeUserID, lang, text string) strin } evidence := a.collectTraderDiagnosisEvidence(storeUserID, target.ID, target.Name) - if answer, ok := a.generateTraderDiagnosisAnswerWithLLM(context.Background(), lang, text, evidence); ok { + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + if answer, ok := a.generateTraderDiagnosisAnswerWithLLM(ctx, lang, text, evidence); ok { return answer } return formatTraderDiagnosisEvidence(lang, evidence)