From d63322244cde6de8e8ef525dc0bd8861074c381b Mon Sep 17 00:00:00 2001 From: AskaEth Date: Sun, 28 Jun 2026 17:19:03 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20performThink=20defer=20unlock=E6=94=B9?= =?UTF-8?q?=E5=9B=9E=E7=AB=8B=E5=8D=B3unlock=E2=80=94=E2=80=94LLM=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E6=9C=9F=E9=97=B4=E4=B8=8D=E8=83=BD=E6=8C=81=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/ai-core/internal/background/thinker.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/backend/ai-core/internal/background/thinker.go b/backend/ai-core/internal/background/thinker.go index 2a2ca3c..b0de429 100644 --- a/backend/ai-core/internal/background/thinker.go +++ b/backend/ai-core/internal/background/thinker.go @@ -1080,8 +1080,8 @@ func (t *Thinker) HasPendingThoughts() bool { // 强制执行最小间隔,防止并发调用或 bug 导致 LLM 配额被快速消耗。 func (t *Thinker) performThink(triggerReason string) { t.muLock() - defer t.muUnlock() gapSinceLast := time.Since(t.lastThinkTimeAtomic()) + t.muUnlock() minGap := t.minThinkGap if minGap <= 0 { minGap = 5 * time.Second // 默认最小间隔 5 秒 @@ -1101,15 +1101,14 @@ func (t *Thinker) performThink(triggerReason string) { ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second) defer cancel() t.muLock() - defer t.muUnlock() t.thinkCancel = cancel - + t.muUnlock() // 0. 让步于前台——如果用户最近有活动(非post_chat),跳过本次思考。 if triggerReason != "post_chat" { t.muLock() - defer t.muUnlock() sinceLastUser := time.Since(t.lastUserTime()) + t.muUnlock() if sinceLastUser < 10*time.Second { log.Printf("[后台思考] 用户 %v 前有活动,让步于前台回复,跳过思考", sinceLastUser.Round(time.Second)) @@ -1122,8 +1121,8 @@ func (t *Thinker) performThink(triggerReason string) { // 0. 让步于前台——如果用户最近有活动(非post_chat),跳过本次思考。 if triggerReason != "post_chat" { t.muLock() - defer t.muUnlock() sinceLastUser := time.Since(t.lastUserTime()) + t.muUnlock() if sinceLastUser < 10*time.Second { log.Printf("[后台思考] 用户 %v 前有活动,让步于前台回复,跳过思考", sinceLastUser.Round(time.Second))