Skip to content

WorkflowTask failure because of an invalid state transition in LocalActivity state machine #2155

@Quinn-With-Two-Ns

Description

@Quinn-With-Two-Ns

The following test case should pass

public class LocalActivityAfterCancelTest {
  private final TestActivitiesImpl activitiesImpl = new TestActivitiesImpl();

  @Rule
  public SDKTestWorkflowRule testWorkflowRule =
      SDKTestWorkflowRule.newBuilder()
          .setUseExternalService(true)
          .setWorkflowTypes(TestLocalActivityRetry.class, BlockingWorkflow.class)
          .setActivityImplementations(activitiesImpl)
          .build();

  @Test
  public void localActivityAfterChildWorkflowCanceled() {
    TestWorkflow1 workflowStub =
        testWorkflowRule.newWorkflowStubTimeoutOptions(TestWorkflow1.class);
    WorkflowClient.execute(workflowStub::execute, "input");
    WorkflowStub.fromTyped(workflowStub).cancel();
    WorkflowFailedException exception =
        Assert.assertThrows(WorkflowFailedException.class, () -> workflowStub.execute("input"));
    Assert.assertEquals(
        EventType.EVENT_TYPE_WORKFLOW_EXECUTION_CANCELED, exception.getWorkflowCloseEventType());
  }

  @Test
  public void testLocalActivityAfterChildWorkflowCanceledReplay() {
    assertThrows(
        RuntimeException.class,
        () ->
            WorkflowReplayer.replayWorkflowExecutionFromResource(
                "testLocalActivityAfterCancelTest.json",
                LocalActivityAfterCancelTest.TestLocalActivityRetry.class));
  }

  @WorkflowInterface
  public static class BlockingWorkflow implements TestWorkflows.TestWorkflowReturnString {
    @Override
    public String execute() {
      Workflow.await(() -> false);
      return "";
    }
  }

  public static class TestLocalActivityRetry implements TestWorkflow1 {

    @Override
    public String execute(String taskQueue) {
      try {
        ChildWorkflowOptions childOptions =
            ChildWorkflowOptions.newBuilder()
                .setWorkflowId(Workflow.getInfo().getWorkflowId() + "-child1")
                .setCancellationType(ChildWorkflowCancellationType.WAIT_CANCELLATION_REQUESTED)
                .setParentClosePolicy(ParentClosePolicy.PARENT_CLOSE_POLICY_REQUEST_CANCEL)
                .validateAndBuildWithDefaults();
        TestWorkflows.TestWorkflowReturnString child =
            Workflow.newChildWorkflowStub(
                TestWorkflows.TestWorkflowReturnString.class, childOptions);
        child.execute();
      } catch (TemporalFailure e) {
        if (CancellationScope.current().isCancelRequested()) {
          Workflow.newDetachedCancellationScope(
                  () -> {
                    VariousTestActivities act =
                        Workflow.newLocalActivityStub(
                            VariousTestActivities.class,
                            LocalActivityOptions.newBuilder()
                                .setStartToCloseTimeout(Duration.ofSeconds(5))
                                .validateAndBuildWithDefaults());
                    act.activity1(10);
                  })
              .run();
          throw e;
        }
      }
      return "ignored";
    }
  }
}

It fails with an error like

	LocalActivity: invalid REQUEST_PREPARED->NON_REPLAY_WORKFLOW_TASK_STARTED, transition history is [CREATED->CHECK_EXECUTION_STATE, EXECUTING->SCHEDULE]

See also: https://community.temporal.io/t/workflowtask-failure-because-of-an-invalid-state-transition-in-localactivity-state-machine/12895/3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions