# Parent and child workflow close policy

**URL:** https://community.temporal.io/t/parent-and-child-workflow-close-policy/463
**Category:** Community Support
**Tags:** go-sdk
**Created:** [August 14, 2020, 5:34pm UTC](https://community.temporal.io/t/parent-and-child-workflow-close-policy/463 "2020-08-14T17:34:40Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![pauldemarco](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/pauldemarco/32/19_2.png) [@pauldemarco](https://community.temporal.io/u/pauldemarco)
#### Post date: [August 14, 2020, 5:34pm UTC](https://community.temporal.io/t/parent-and-child-workflow-close-policy/463/1 "2020-08-14T17:34:40Z")

</div>

The docs state:

> When a parent workflow is cancelled by the user, the child workflow can be cancelled or abandoned based on a configurable child policy.

If the policy is to cancel child workflows; what happens if the parent workflow returns an error? Do the children also terminate?

---

<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: [August 14, 2020, 5:53pm UTC](https://community.temporal.io/t/parent-and-child-workflow-close-policy/463/2 "2020-08-14T17:53:00Z")

</div>

I believe `ChildWorkflowClose` policy was never implemented and is not even available at least in Temporal. Instead [ParentClosePolicy](https://github.com/temporalio/go-sdk/blob/478fbb8272101e7ed3babd3a9d3e1813bc513af8/internal/workflow.go#L241) was introduced. It defines child behavior in case of parent completion. This allows specifying different policies for different children.

The behavior of the child doesn’t depend on the reason the parent completed. So it doesn’t matter if the parent completed successfully, failed, or timed out. The child will be affected according to the policy:

- `PARENT_CLOSE_POLICY_TERMINATE`: child workflow is terminated
- `PARENT_CLOSE_POLICY_ABANDON`: the child is not notified about parent completion
- `PARENT_CLOSE_POLICY_REQUEST_CANCEL`: the child gets the cancellation request

There is one more concept that applies to the parent-child relationship. A parent can request a child cancellation by canceling context passed to the `ExecuteChildWorkflow` call. The `WaitFoCancellation` parameter defines if the future returned from the `ExecuteChildWorkflow` becomes ready immediately or only after the child completes its execution.

---

<div class="post-metadata">

### Author: ![Bryan](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/bryan/32/3245_2.png) [@Bryan](https://community.temporal.io/u/Bryan)
#### Post date: [February 22, 2023, 8:53am UTC](https://community.temporal.io/t/parent-and-child-workflow-close-policy/463/3 "2023-02-22T08:53:26Z")

</div>

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](http://go.temporal.io/api) v1.11.1-0.20220907050538-6de5285cf463  
- [go.temporal.io/sdk](http://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](http://go.temporal.io/api) v1.13.0  
- [go.temporal.io/sdk](http://go.temporal.io/sdk) v1.18.1

Below is the code snippet:

```auto
> 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)
> }

```

1. 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.

```auto
childOptions := workflow.ChildWorkflowOptions{
	...
	WaitForCancellation: true,
}

```

- I defined a cancel handler:

```auto
childCtx, cancelHandler := workflow.WithCancel(ctx)

```

- Then I called the handler when the timer is triggered:

```auto
cancelHandler()

```

However, the child workflow is still running. I also tried to use childCtx.Done() but it does not work.

---

<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: [February 22, 2023, 4:46pm UTC](https://community.temporal.io/t/parent-and-child-workflow-close-policy/463/4 "2023-02-22T16:46:38Z")

</div>

Bryan,  
Next time open a separate topic for a new issue, please.

Look at the workflow execution histories of both workflows in both cases and see if they are different.

---

<div class="post-metadata">

### Author: ![Bryan](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/bryan/32/3245_2.png) [@Bryan](https://community.temporal.io/u/Bryan)
#### Post date: [February 23, 2023, 4:18am UTC](https://community.temporal.io/t/parent-and-child-workflow-close-policy/463/5 "2023-02-23T04:18:22Z")

</div>

Hi Maxim,  
Alright, let me move my question to a new topic then.
