Skip to content

Commit aad69a1

Browse files
committed
[bugfix] Synchronize access to state between publish and write
1 parent 3660f81 commit aad69a1

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

internal/app/controller/controller.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type GameController struct {
2828
outstandingTeamChoice *TeamChoice
2929
ConnectionMutex sync.Mutex
3030
PublishMutex sync.Mutex
31+
StateMutex sync.Mutex
3132
VisionReceiver *vision.Receiver
3233
}
3334

@@ -118,12 +119,16 @@ func (c *GameController) setupTimeProvider() {
118119
func (c *GameController) updateLoop() {
119120
for {
120121
time.Sleep(time.Millisecond * 10)
122+
c.StateMutex.Lock()
121123
c.update()
124+
c.StateMutex.Unlock()
122125
}
123126
}
124127

125128
// updateCi updates the current time to the given time and returns the updated referee message
126129
func (c *GameController) updateCi(t time.Time) *refproto.Referee {
130+
c.StateMutex.Lock()
131+
defer c.StateMutex.Unlock()
127132
c.Engine.TimeProvider = func() time.Time { return t }
128133
c.update()
129134
c.Publisher.Publish(c.Engine.State)
@@ -178,13 +183,16 @@ func (c *GameController) updateOnlineStates() {
178183
func (c *GameController) publishToNetwork() {
179184
for {
180185
time.Sleep(25 * time.Millisecond)
186+
c.StateMutex.Lock()
181187
c.Publisher.Publish(c.Engine.State)
188+
c.StateMutex.Unlock()
182189
}
183190
}
184191

185192
// OnNewEvent processes the given event
186193
func (c *GameController) OnNewEvent(event Event) {
187-
194+
c.StateMutex.Lock()
195+
defer c.StateMutex.Unlock()
188196
if event.GameEvent != nil && !c.Engine.disabledGameEvent(event.GameEvent.Type) && c.askForTeamDecisionIfRequired(event) {
189197
return
190198
}

0 commit comments

Comments
 (0)