Skip to content

Commit

Permalink
[core] OCTRL-889 proper usage of mutex in environment.go
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Tichák authored and teo committed Apr 30, 2024
1 parent 6e72bea commit ede577b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 9 additions & 1 deletion core/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ func newEnvironment(userVars map[string]string, newId uid.ID) (env *Environment,
},
fsm.Callbacks{
"before_event": func(_ context.Context, e *fsm.Event) {

env.Mu.Lock()
env.currentTransition = e.Event
env.Mu.Unlock()

trigger := fmt.Sprintf("before_%s", e.Event)

the.EventWriterWithTopic(topic.Environment).WriteEvent(&pb.Ev_EnvironmentEvent{
Expand Down Expand Up @@ -382,7 +386,11 @@ func newEnvironment(userVars map[string]string, newId uid.ID) (env *Environment,
}).Debug("environment.sm entering state")
},
"after_event": func(_ context.Context, e *fsm.Event) {
defer func() { env.currentTransition = "" }()
defer func() {
env.Mu.Lock()
env.currentTransition = ""
env.Mu.Unlock()
}()

trigger := fmt.Sprintf("after_%s", e.Event)

Expand Down
2 changes: 2 additions & 0 deletions core/environment/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,9 @@ func (envs *Manager) TeardownEnvironment(environmentId uid.ID, force bool) error
return errors.New(fmt.Sprintf("cannot teardown environment in state %s", env.CurrentState()))
}

env.Mu.Lock()
env.currentTransition = "DESTROY"
env.Mu.Unlock()

err = env.handleHooks(env.Workflow(), "leave_"+env.CurrentState())
if err != nil {
Expand Down

0 comments on commit ede577b

Please sign in to comment.