Fixed race condition on workflow start in TestWorkflowService
@@ -188,9 +188,6 @@ public final class TestWorkflowService implements IWorkflowService {
lock.lock();
try {
existing = executionsByWorkflowId.get(workflowId);
- } finally {
- lock.unlock();
- }
if (existing != null) {
Optional<WorkflowExecutionCloseStatus> statusOptional = existing.getCloseStatus();
WorkflowIdReusePolicy policy =
@@ -207,8 +204,11 @@ public final class TestWorkflowService implements IWorkflowService {
return throwDuplicatedWorkflow(startRequest, existing);
}
}
- return startWorkflowExecutionNoRunningCheck(
+ return startWorkflowExecutionNoRunningCheckLocked(
startRequest, parent, parentChildInitiatedEventId, workflowId);
+ } finally {
+ lock.unlock();
+ }
}
private StartWorkflowExecutionResponse throwDuplicatedWorkflow(
@@ -224,7 +224,7 @@ public final class TestWorkflowService implements IWorkflowService {
throw error;
}
- private StartWorkflowExecutionResponse startWorkflowExecutionNoRunningCheck(
+ private StartWorkflowExecutionResponse startWorkflowExecutionNoRunningCheckLocked(
StartWorkflowExecutionRequest startRequest,
Optional<TestWorkflowMutableState> parent,
OptionalLong parentChildInitiatedEventId,
@@ -236,13 +236,8 @@ public final class TestWorkflowService implements IWorkflowService {
startRequest, parent, parentChildInitiatedEventId, this, store);
WorkflowExecution execution = result.getExecutionId().getExecution();
ExecutionId executionId = new ExecutionId(domain, execution);
- lock.lock();
- try {
executionsByWorkflowId.put(workflowId, result);
executions.put(executionId, result);
- } finally {
- lock.unlock();
- }
result.startWorkflow();
return new StartWorkflowExecutionResponse().setRunId(execution.getRunId());
}
@@ -464,10 +459,15 @@ public final class TestWorkflowService implements IWorkflowService {
.setWorkflowId(executionId.getWorkflowId().getWorkflowId())
.setWorkflowIdReusePolicy(previousRunStartRequest.getWorkflowIdReusePolicy())
.setIdentity(identity);
+ lock.lock();
+ try {
StartWorkflowExecutionResponse response =
- startWorkflowExecutionNoRunningCheck(
+ startWorkflowExecutionNoRunningCheckLocked(
startRequest, parent, parentChildInitiatedEventId, executionId.getWorkflowId());
return response.getRunId();
+ } finally {
+ lock.unlock();
+ }
}
@Override
