From eff0f491e9afcd24fb2dbecb1b58cb4c168571ee Mon Sep 17 00:00:00 2001 From: Together Date: Thu, 12 Feb 2026 01:26:22 +0800 Subject: [PATCH] fix(agent): use atomic.Bool for AgentLoop.running to prevent data race Run() and Stop() access the `running` field from different goroutines without synchronization. Replace the bare `bool` with `sync/atomic.Bool` to eliminate the data race. --- pkg/agent/loop.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/agent/loop.go b/pkg/agent/loop.go index 40c9ba7..cc14cea 100644 --- a/pkg/agent/loop.go +++ b/pkg/agent/loop.go @@ -14,6 +14,7 @@ import ( "path/filepath" "strings" "sync" + "sync/atomic" "time" "github.com/sipeed/picoclaw/pkg/bus" @@ -35,7 +36,7 @@ type AgentLoop struct { sessions *session.SessionManager contextBuilder *ContextBuilder tools *tools.ToolRegistry - running bool + running atomic.Bool 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, contextBuilder: contextBuilder, tools: toolsRegistry, - running: false, summarizing: sync.Map{}, } } func (al *AgentLoop) Run(ctx context.Context) error { - al.running = true + al.running.Store(true) - for al.running { + for al.running.Load() { select { case <-ctx.Done(): return nil @@ -138,7 +138,7 @@ func (al *AgentLoop) Run(ctx context.Context) error { } func (al *AgentLoop) Stop() { - al.running = false + al.running.Store(false) } func (al *AgentLoop) RegisterTool(tool tools.Tool) {