@@ -172,14 +172,14 @@ func startRemoving(ctx context.Context, sbx *memorySandbox, opts sandbox.RemoveO
172172 }
173173 }
174174
175+ originalState := sbx ._data .State
175176 newState := opts .Action .TargetState
176177
177178 if transition != nil {
178- currentState := sbx ._data .State
179179 sbx .mu .Unlock ()
180180
181- if currentState != newState && ! sandbox.AllowedTransitions [currentState ][newState ] {
182- return false , nil , & sandbox.InvalidStateTransitionError {CurrentState : currentState , TargetState : newState }
181+ if originalState != newState && ! sandbox.AllowedTransitions [originalState ][newState ] {
182+ return false , nil , & sandbox.InvalidStateTransitionError {CurrentState : originalState , TargetState : newState }
183183 }
184184
185185 logger .L ().Debug (ctx , "State transition already in progress to the same state, waiting" , logger .WithSandboxID (sbx .SandboxID ()), zap .String ("state" , string (newState )))
@@ -190,9 +190,9 @@ func startRemoving(ctx context.Context, sbx *memorySandbox, opts sandbox.RemoveO
190190
191191 // If the transition is to the same state just wait
192192 switch {
193- case currentState == newState :
193+ case originalState == newState :
194194 return true , func (context.Context , error ) {}, nil
195- case sandbox.AllowedTransitions [currentState ][newState ]:
195+ case sandbox.AllowedTransitions [originalState ][newState ]:
196196 return startRemoving (ctx , sbx , sandbox.RemoveOpts {Action : opts .Action })
197197 default :
198198 return false , nil , fmt .Errorf ("unexpected state transition" )
@@ -238,11 +238,11 @@ func startRemoving(ctx context.Context, sbx *memorySandbox, opts sandbox.RemoveO
238238 }
239239
240240 if err != nil {
241- // Keep the transition in place so the error stays
242- return
241+ // Revert the state change if the transition failed and it's not a transient transition
242+ sbx . _data . State = originalState
243243 }
244244
245- // The transition is completed and the next transition can be started
245+ // Remove the transition so the next transition can be started
246246 sbx .transition = nil
247247 }
248248
0 commit comments