Why Is There a Delay in Workflow Cancellation

I wrote an Activity to simulate a long-running task.

// workflow.ActivityOptions{
// 	ScheduleToCloseTimeout: time.Minute * 30,
// 	HeartbeatTimeout:       time.Minute * 2,
// 	WaitForCancellation:    true,
// })
func Activity(ctx context.Context) error {  
    for range time.Tick(time.Second) {  
       if err := ctx.Err(); err != nil {  
          return ctx.Err()  
       }  
  
       activity.RecordHeartbeat(ctx)  
       logger.Info("task is running")  
    }  
    return nil  
}

After executing a Workflow that includes this Activity, the log continuously prints “task is running”. Then, after executing Cancel Workflow, the screen still prints messages until about 30 to 50 seconds later, when the task is finally cancelled.

Since I executed RecordHeartbeat every second, I expected it to be able to receive the cancellation in 1 second.

Heartbeats are throttled. They are emitted at each “0.8 * heartbeat interval” intervals. This is done to avoid overloading the service. We do plan to add async cancellation notification in the future to bring cancellation time down.