Hi Maxim. I’d like to ask about a parent canceling/terminating its child workflow execution.
There is parent workflow A executes a child workflow A with a timer. If the timer is triggered (timeout), the child wf is cancelled/terminated. Workflow A uses below versions:
- go.temporal.io/api v1.11.1-0.20220907050538-6de5285cf463
- go.temporal.io/sdk v1.17.0
Then, there is another parent workflow B, it executes a child workflow B with a timer. If the timer is triggered (timeout), the child wf should be cancelled/terminated. When I test it, the cwf is not cancelled although workflow B has the same code as workflow A. Workflow B uses below versions:
- go.temporal.io/api v1.13.0
- go.temporal.io/sdk v1.18.1
Below is the code snippet:
> childOptions := workflow.ChildWorkflowOptions{
> 	WorkflowID:            childWorkflowId,
> 	RetryPolicy:           retryPolicy,
> 	SearchAttributes:      searchAttributes,
> 	TaskQueue:             wf.TaskQueue,
> 	WorkflowIDReusePolicy: enums.WORKFLOW_ID_REUSE_POLICY_ALLOW_DUPLICATE,
> }
> ctx = workflow.WithChildOptions(ctx, childOptions)
> 
> childFuture := workflow.ExecuteChildWorkflow(ctx, utility.ChildWorkflowName, childWfReq)
> 
> loopCtx := workflow.WithActivityOptions(ctx, activityOptions)
> 	timerCtx, timerCancel := workflow.WithCancel(loopCtx)
> 	timer := workflow.NewTimer(timerCtx, dynamicConfig.Timer)
> 	selector := workflow.NewSelector(timerCtx)
> 	selector.AddFuture(timer, func(f workflow.Future) {
> 		err1 := f.Get(timerCtx, nil)
> 		if loopCtx.Err() != nil {
> 			logger.Error("Error occurred.", "error", loopCtx.Err())
> 		}
> 	}).AddFuture(childFuture, func(f workflow.Future) {
> 		err := childFuture.Get(ctx, &request)
> 		if err != nil {
> 			logger.Error("Error occurred getting child workflow result.", "error", err)
> 		}
> 		timerCancel()
> 	}).Select(loopCtx)
> }
- Could the different behaviors caused by different sdk/api versions?
 
I tried to modify workflow B to manually cancel the child workflow:
- I added WaitForCancellation.
 
childOptions := workflow.ChildWorkflowOptions{
	...
	WaitForCancellation:   true,
}
- I defined a cancel handler:
 
childCtx, cancelHandler := workflow.WithCancel(ctx)
- Then I called the handler when the timer is triggered:
 
cancelHandler()
However, the child workflow is still running. I also tried to use childCtx.Done() but it does not work.