Merge pull request #30 from DevEverything01/fix/atomic-running

fix(agent): use atomic.Bool for AgentLoop.running to prevent data race
This commit is contained in:
yinwm
2026-02-12 10:21:39 +08:00
committed by GitHub

View File

@@ -14,6 +14,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
"sync/atomic"
"time" "time"
"github.com/sipeed/picoclaw/pkg/bus" "github.com/sipeed/picoclaw/pkg/bus"
@@ -35,7 +36,7 @@ type AgentLoop struct {
sessions *session.SessionManager sessions *session.SessionManager
contextBuilder *ContextBuilder contextBuilder *ContextBuilder
tools *tools.ToolRegistry tools *tools.ToolRegistry
running bool running atomic.Bool
summarizing sync.Map // Tracks which sessions are currently being summarized summarizing sync.Map // Tracks which sessions are currently being summarized
} }
@@ -101,15 +102,14 @@ func NewAgentLoop(cfg *config.Config, msgBus *bus.MessageBus, provider providers
sessions: sessionsManager, sessions: sessionsManager,
contextBuilder: contextBuilder, contextBuilder: contextBuilder,
tools: toolsRegistry, tools: toolsRegistry,
running: false,
summarizing: sync.Map{}, summarizing: sync.Map{},
} }
} }
func (al *AgentLoop) Run(ctx context.Context) error { func (al *AgentLoop) Run(ctx context.Context) error {
al.running = true al.running.Store(true)
for al.running { for al.running.Load() {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return nil return nil
@@ -138,7 +138,7 @@ func (al *AgentLoop) Run(ctx context.Context) error {
} }
func (al *AgentLoop) Stop() { func (al *AgentLoop) Stop() {
al.running = false al.running.Store(false)
} }
func (al *AgentLoop) RegisterTool(tool tools.Tool) { func (al *AgentLoop) RegisterTool(tool tools.Tool) {