Difference between Cancel and Terminate in Workflow

When workflow is cancelled or terminated, in both the cases, temporalio.exceptions.CancelledError is raised.
What’s the difference between cancel and terminate as in both the cases activity is able to perform a cleanup?

PS: Activity I have implemented are sync python activities, worker is instantiated with activity_executor in order to run non-async activities, and worker graceful timeout is set to a very high duration…

PS: Other post mentions that termination is a hard stop of workflow, but that is not happening in this case
References: Difference between Cancel and Terminate - #2 by maxim

Termination is a hard stop of a workflow, not the worker. Please provide more information about what exactly is happening. Workflow history would help.

I am sharing activity code

def run(self):
        try:
            for pct in range(0, 100, 1):
                activity.heartbeat(pct)
                # below sleep is similar to grpc/any client/database call
                # PS: time.sleep() is used just to emulate network/blocking calls, we should refrain from using sleep
                time.sleep(0.5)
        except CancelledError as ce:
            LOG.info(f'Received the error, {type(ce)}')
            # reraising the exception
            raise ce

This is how my worker params looks like

Worker(
   client,
   graceful_shutdown_timeout=timedelta(days=30),
   activity_executor=ThreadPoolExecutor(max_workers=10)
)

I did two experiments

  1. Cancel the workflow running this activity.
  2. Terminate the workflow running this activity.

I expected Received the error... log to be printed only in experiment 1.
But even in experiment 2, activity is catching the CancelledError.

Terminating workflow (or completing it any other way) cancels all its activities. Cancellation gives the workflow an ability to perform whatever business logic is needed for cleanup.

1 Like