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 <noreply@anthropic.com>
This commit is contained in:
shinchan-zhai
2026-05-11 16:37:30 +08:00
parent e67a927a4f
commit 94844b7139
2 changed files with 10 additions and 1 deletions

View File

@@ -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)
}()

View File

@@ -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)