Skip to content

Commit

Permalink
Protect 'currentState' variable using Mutex to prevent race condition.
Browse files Browse the repository at this point in the history
  • Loading branch information
urvisavla committed Jun 1, 2023
1 parent 148bf79 commit 5ac40f1
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion services/horizon/internal/ingest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ type system struct {

reapOffsets map[string]int64

currentState State
currentStateMutex sync.Mutex
currentState State
}

func NewSystem(config Config) (System, error) {
Expand Down Expand Up @@ -484,6 +485,8 @@ func (s *system) initMetrics() {
}

func (s *system) GetCurrentState() State {
s.currentStateMutex.Lock()
defer s.currentStateMutex.Unlock()
return s.currentState
}

Expand Down Expand Up @@ -652,7 +655,10 @@ func (s *system) runStateMachine(cur stateMachineNode) error {
panic("unexpected transaction")
}

s.currentStateMutex.Lock()
s.currentState = cur.GetState()
s.currentStateMutex.Unlock()

next, err := cur.run(s)
if err != nil {
logger := log.WithFields(logpkg.F{
Expand Down

0 comments on commit 5ac40f1

Please sign in to comment.