# When cancelled the workflow does not complete. It continues to be in the running state

**URL:** <https://community.temporal.io/t/when-cancelled-the-workflow-does-not-complete-it-continues-to-be-in-the-running-state/6203>\
**Category:** Community Support\
**Tags:** java-sdk\
**Created:** [October 12, 2022, 3:10pm UTC](https://community.temporal.io/t/when-cancelled-the-workflow-does-not-complete-it-continues-to-be-in-the-running-state/6203 "2022-10-12T15:10:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anjula\_Paulus](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/anjula_paulus/32/1715_2.png) [@Anjula\_Paulus](https://community.temporal.io/u/Anjula_Paulus)\
**Post date:** [October 12, 2022, 3:10pm UTC](https://community.temporal.io/t/when-cancelled-the-workflow-does-not-complete-it-continues-to-be-in-the-running-state/6203/1 "2022-10-12T15:10:35Z")

</div>

I guess this is due to the await block. Is there better way to do it? Need to make the boolean isWorkflowDone thread safe from multiple access from threads. Please provide your valuable feedback.

```auto
public class GenericWorkflowImpl implements GenericWorkflow {
    private boolean isWorkflowDone = false;
    private final RetryOptions retryoptions = RetryOptions.newBuilder()
            .setInitialInterval(Duration.ofSeconds(1))
            .setMaximumInterval(Duration.ofSeconds(100))
            .setBackoffCoefficient(2)
            .setMaximumAttempts(10)
            .build();
    private final ActivityOptions options = ActivityOptions.newBuilder()
            .setStartToCloseTimeout(Duration.ofSeconds(30))
            .setRetryOptions(retryoptions)
            .setCancellationType(ActivityCancellationType.TRY_CANCEL)
            .build();

    private final GenericActivity genericActivity = Workflow.newActivityStub(GenericActivity.class, options);

    @Override
    public void start(final WorkflowType workflowType, final String taskType, final TaskInfo payload) {
        genericActivity.createNewTask(workflowType, taskType, payload);
        Workflow.await(() -> isWorkflowDone);
    }

    @Override
    public void approveChange() {
        genericActivity.approveTaskChange();
        genericActivity.updateTaskStatusChange();
        this.isWorkflowDone = true;
    }

    @Override
    public void rejectChange() {
        CancellationScope scope = Workflow.newCancellationScope(() -> {
            genericActivity.updateTaskStatusChange();
        });
        scope.run();
        scope.cancel();
    }
}

```

---

<div class="post-metadata">

**Author:** ![tihomir](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/tihomir/32/6580_2.png) [@tihomir](https://community.temporal.io/u/tihomir)\
**Post date:** [October 12, 2022, 5:51pm UTC](https://community.temporal.io/t/when-cancelled-the-workflow-does-not-complete-it-continues-to-be-in-the-running-state/6203/2 "2022-10-12T17:51:47Z")

</div>

Hi, can you give a little more info on your use case? In your rejectChange signal handler you are invoking and then canceling the updateTaskStatusChange activity that is in the CancellationScope. Would like to understand what you would like to do.

---

<div class="post-metadata">

**Author:** ![Anjula\_Paulus](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/anjula_paulus/32/1715_2.png) [@Anjula\_Paulus](https://community.temporal.io/u/Anjula_Paulus)\
**Post date:** [October 13, 2022, 4:48am UTC](https://community.temporal.io/t/when-cancelled-the-workflow-does-not-complete-it-continues-to-be-in-the-running-state/6203/3 "2022-10-13T04:48:57Z")

</div>

@tihomir The usecase is that the user would be able to either approve or reject a workflow.

1. If approved. The workflow would be completed successfully.
2. If rejected. I want it the workflow to be cancelled.  
I tried setting `this.isWorkflowDone = true;` in reject change which will complete the workflow and also not thread safe.

Is there a better way to approach this usecase? I want the workflow to be cancelled not completed.

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [October 13, 2022, 2:44pm UTC](https://community.temporal.io/t/when-cancelled-the-workflow-does-not-complete-it-continues-to-be-in-the-running-state/6203/4 "2022-10-13T14:44:46Z")

</div>

Workflow canceling itself doesn’t make sense in Temporal. Cancellation, by definition, is an external request to a workflow from outside.

So if you want your workflow to be canceled, then you want to request cancelation through the client instead of sending _rejectChange_ signal.

---

<div class="post-metadata">

**Author:** ![Anjula\_Paulus](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/anjula_paulus/32/1715_2.png) [@Anjula\_Paulus](https://community.temporal.io/u/Anjula_Paulus)\
**Post date:** [October 17, 2022, 6:11pm UTC](https://community.temporal.io/t/when-cancelled-the-workflow-does-not-complete-it-continues-to-be-in-the-running-state/6203/5 "2022-10-17T18:11:23Z")

</div>

@maxim @tihomir Is there a example of sending a cancellation request through the client? Im kindof new to the whole thing.

---

<div class="post-metadata">

**Author:** ![tihomir](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/tihomir/32/6580_2.png) [@tihomir](https://community.temporal.io/u/tihomir)\
**Post date:** [October 17, 2022, 6:18pm UTC](https://community.temporal.io/t/when-cancelled-the-workflow-does-not-complete-it-continues-to-be-in-the-running-state/6203/6 "2022-10-17T18:18:56Z")

</div>

We don’t have a sample for it currently in the [java samples repo](https://github.com/temporalio/samples-java) but will add.

You could do something like (typed stub)

```auto
MyWorkflow workflow =
      client.newWorkflowStub(MyWorkflow.class,"<workflowId>");

WorkflowStub.fromTyped(myWorkflow).cancel();

```

or untyped

```auto
WorkflowStub stub =
        client.newUntypedWorkflowStub("<workflowid>", Optional.empty(), Optional.empty());
stub.cancel();

```

---

<div class="post-metadata">

**Author:** ![Anjula\_Paulus](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/anjula_paulus/32/1715_2.png) [@Anjula\_Paulus](https://community.temporal.io/u/Anjula_Paulus)\
**Post date:** [October 17, 2022, 6:24pm UTC](https://community.temporal.io/t/when-cancelled-the-workflow-does-not-complete-it-continues-to-be-in-the-running-state/6203/7 "2022-10-17T18:24:38Z")

</div>

Thanks for the help.
