fix: performThink defer unlock改回立即unlock——LLM调用期间不能持锁

This commit is contained in:
2026-06-28 17:19:03 +08:00
parent d4c246fa0c
commit d63322244c
@@ -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))