Cancelled workflow waiting for signals


I’ve a workflow which waits on some signal and executes some activities. My use case is to restart the workflow based on an external event. The solution which i devised was to use CancelWorkflow() API and return NewContinueAsNewError() in case of cancel error in the executeActivity section.

The problem is, Since the workflow is blocked on signals it’s still waiting on it, even if the cancel event is received on the workflow.

As soon it receives the expected signals it goes on executing the activity and fails because of cancellation call and return continueAsNew error. (which is the expectation)

Is there a way to cancel and use continueAsNew when the signals are waiting?

func SampleWorkflow(ctx workflow.Context) error {

signalChannel := workflow.GetSignalChannel(ctx, "some-chan-1")
received := signalChannel.Receive(ctx, nil)
if received {
	//perform some action

signalChannel2 := workflow.GetSignalChannel(ctx, "some-chan-2")
received = signalChannel2.Receive(ctx, nil)
if received {
	//perform some action

err := workflow.ExecuteActivity(aCtx, someActivity).Get(aCtx, nil)
if err != nil {
	return workflow.NewContinueAsNewError(ctx, SampleWorkflow, nil)
return nil}

PS:- Tried using TerminateWorkflow() as well, but this closes the workflow with termination error and doesn’t return the continueAsNew.

Thank you :slight_smile:

Use Selector to wait on both external signal and the ctx.Done channels.

1 Like

Thanks a lot for your quick response @maxim . It helps :slight_smile: