Canceling workflow.go routine

Hi all,

Referring to this topic 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:

Stack trace active go routine blocked, waiting for signal:


image
image
image

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?

> 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) {
> 	...
> }

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?

I believe AwaitWithTimeout unblocks when its context is canceled.