From ba8353367a8f496dbff358fb07539104409e9618 Mon Sep 17 00:00:00 2001 From: Janne Valkealahti Date: Sat, 21 May 2016 21:21:02 +0100 Subject: [PATCH] Add locking to state handling - We still got trouble with #227 with smoke tests so adding some locking sync to state handling. --- .../support/AbstractStateMachine.java | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/AbstractStateMachine.java b/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/AbstractStateMachine.java index 05f2594c..93bd3990 100644 --- a/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/AbstractStateMachine.java +++ b/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/AbstractStateMachine.java @@ -111,6 +111,8 @@ public abstract class AbstractStateMachine extends StateMachineObjectSuppo private volatile Message forwardedInitialEvent; + private final Object lock = new Object(); + /** * Instantiates a new abstract state machine. * @@ -165,10 +167,12 @@ public abstract class AbstractStateMachine extends StateMachineObjectSuppo public State getState() { // if we're complete assume we're stopped // and state was stashed into lastState - if (lastState != null && isComplete()) { - return lastState; - } else { - return currentState; + synchronized (lock) { + if (lastState != null && isComplete()) { + return lastState; + } else { + return currentState; + } } } @@ -342,13 +346,15 @@ public abstract class AbstractStateMachine extends StateMachineObjectSuppo @Override protected void doStop() { - stateMachineExecutor.stop(); - notifyStateMachineStopped(buildStateContext(Stage.STATEMACHINE_STOP, null, null, this)); - // stash current state before we null it so that - // we can still return where we 'were' when machine is stopped - lastState = currentState; - currentState = null; - initialEnabled = null; + synchronized (lock) { + stateMachineExecutor.stop(); + notifyStateMachineStopped(buildStateContext(Stage.STATEMACHINE_STOP, null, null, this)); + // stash current state before we null it so that + // we can still return where we 'were' when machine is stopped + lastState = currentState; + currentState = null; + initialEnabled = null; + } } @Override