loop: fix data race in Agent.CurrentStateName()
The CurrentStateName() method was directly accessing the stateMachine's
currentState field without proper synchronization, causing data races
with TransitionWithEvent() which writes to the same field while holding
a write lock.
The StateMachine struct already provides a thread-safe CurrentState()
method that properly acquires a read lock before accessing currentState.
This fix changes CurrentStateName() from:
return a.stateMachine.currentState.String()
to:
return a.stateMachine.CurrentState().String()
This eliminates the race condition between HTTP handler goroutines
reading state for SSE streams and the agent loop goroutine writing
state during transitions.
Co-Authored-By: sketch <hello@sketch.dev>
Change-ID: sadf9961b9f084fa7
diff --git a/loop/agent.go b/loop/agent.go
index 909700e..c8891cd 100644
--- a/loop/agent.go
+++ b/loop/agent.go
@@ -453,7 +453,7 @@
if a.stateMachine == nil {
return ""
}
- return a.stateMachine.currentState.String()
+ return a.stateMachine.CurrentState().String()
}
func (a *Agent) URL() string { return a.url }