# Canceling workflow.go routine

**URL:** <https://community.temporal.io/t/canceling-workflow-go-routine/10600>\
**Category:** Community Support\
**Tags:** go-sdk, general-impl\
**Created:** [January 8, 2024, 12:30pm UTC](https://community.temporal.io/t/canceling-workflow-go-routine/10600 "2024-01-08T12:30:32Z")\
**Posts on this page:** 3\
**Page:** 1

<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:** [January 8, 2024, 12:30pm UTC](https://community.temporal.io/t/canceling-workflow-go-routine/10600/1 "2024-01-08T12:30:32Z")

</div>

Hi all,

Referring to this topic [https://community.temporal.io/t/use-case-of-sub-process-and-sample-logic-to-use/7034](https://community.temporal.io/t/use-case-of-sub-process-and-sample-logic-to-use/7034), if I use the workflow.go with awaitWithTimeout inside a loop, do I need to manually close them all? The code works, but when I go to Temporal Web UI → Stack Trace, I can see the go routines are still active. I have used context with cancel and called the cancel() method.

> cancCtx, cancel := workflow.WithCancel(wfCtx)  
> cancel()

The cancelled activities will turn black:

 ![image](https://us1.discourse-cdn.com/flex016/uploads/temporal/original/2X/a/aa275214e38824921e0e960be108ce3c5ae85f96.jpeg)

Stack trace active go routine blocked, waiting for signal:

 ![image](https://us1.discourse-cdn.com/flex016/uploads/temporal/original/2X/1/13b7c7bea2f2016db5fd877f9c33c14948f62da7.png)  
 ![image](https://us1.discourse-cdn.com/flex016/uploads/temporal/original/2X/3/321e3dbd06d4b49a09fd1654038d55b61c59f1c4.png)  
 ![image](https://us1.discourse-cdn.com/flex016/uploads/temporal/original/2X/3/321e3dbd06d4b49a09fd1654038d55b61c59f1c4.png)  
 ![image](https://us1.discourse-cdn.com/flex016/uploads/temporal/original/2X/3/321e3dbd06d4b49a09fd1654038d55b61c59f1c4.png)

How do I close these go routines?

Also, if I have nested condition (go routine inside go routine), when I cancel the context to child process, does it automatically cancel the context for the grandchild process to?

```auto
> func processParent(wfCtx workflow.Context) {
> subProcessDone := false
> cancCtx, cancel := workflow.WithCancel(wfCtx)
> workflow.Go(cancCtx, func(gCtx workflow.Context) {
> processChild(gCtx)
> })
> ok, _ := workflow.AwaitWithTimeout(cancCtx, timer, func() bool {
> return subProcessDone
> })
> cancel()
> }
> 
> func processChild(wfCtx workflow.Context) {
> subProcessDone := false
> cancCtx, cancel := workflow.WithCancel(wfCtx)
> workflow.Go(cancCtx, func(gCtx workflow.Context) {
> processAnotherChild(gCtx)
> })
> ok, _ := workflow.AwaitWithTimeout(cancCtx, timer, func() bool {
> return subProcessDone
> })
> cancel()
> }
> 
> func processAnotherChild(wfCtx workflow.Context) {
> ...
> }

```

---

<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:** [January 8, 2024, 1:29pm UTC](https://community.temporal.io/t/canceling-workflow-go-routine/10600/2 "2024-01-08T13:29:53Z")

</div>

![image](https://us1.discourse-cdn.com/flex016/uploads/temporal/original/2X/1/133fff5cc7006242ace99f34901003245290cb2b.png)

So this is what we are trying to implement. Initially, we use selector for simple cases like ‘Wait Signal A’ and ‘Timer X’. But now, we have nested sub-processes and timers and receive tasks. We tried to implement workflow.Go() and workflow.AwaitWithTimeout() but we encountered issues when cancelling the child contexts. For example, when timer Z timed out, how do I cancel context used in sub-process N?

---

<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:** [January 8, 2024, 8:20pm UTC](https://community.temporal.io/t/canceling-workflow-go-routine/10600/3 "2024-01-08T20:20:12Z")

</div>

I believe AwaitWithTimeout unblocks when its context is canceled.
