Skip to content

Commit fe59662

Browse files
authored
Merge pull request #30 from DevEverything01/fix/atomic-running
fix(agent): use atomic.Bool for AgentLoop.running to prevent data race
2 parents 91e8abf + eff0f49 commit fe59662

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

pkg/agent/loop.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"path/filepath"
1515
"strings"
1616
"sync"
17+
"sync/atomic"
1718
"time"
1819

1920
"github.com/sipeed/picoclaw/pkg/bus"
@@ -35,7 +36,7 @@ type AgentLoop struct {
3536
sessions *session.SessionManager
3637
contextBuilder *ContextBuilder
3738
tools *tools.ToolRegistry
38-
running bool
39+
running atomic.Bool
3940
summarizing sync.Map // Tracks which sessions are currently being summarized
4041
}
4142

@@ -101,15 +102,14 @@ func NewAgentLoop(cfg *config.Config, msgBus *bus.MessageBus, provider providers
101102
sessions: sessionsManager,
102103
contextBuilder: contextBuilder,
103104
tools: toolsRegistry,
104-
running: false,
105105
summarizing: sync.Map{},
106106
}
107107
}
108108

109109
func (al *AgentLoop) Run(ctx context.Context) error {
110-
al.running = true
110+
al.running.Store(true)
111111

112-
for al.running {
112+
for al.running.Load() {
113113
select {
114114
case <-ctx.Done():
115115
return nil
@@ -138,7 +138,7 @@ func (al *AgentLoop) Run(ctx context.Context) error {
138138
}
139139

140140
func (al *AgentLoop) Stop() {
141-
al.running = false
141+
al.running.Store(false)
142142
}
143143

144144
func (al *AgentLoop) RegisterTool(tool tools.Tool) {

0 commit comments

Comments
 (0)