The workflow instance exceeded the CPU usage limit of throttle…

This is a quick one act drama about what happens if you do an infinite loop in a SharePoint 2013 Workflow.

Act 1

Iterating through a set of items in a SharePoint 2013 style workflow is not a one-step action. The infrastructure does not have a ForEach cycle, it can only do Loop with Condition and Loop n Times.

How to iterate through all items? Easy.

You get the number of items you want to iterate through.

Set a pointer.

Do your magic.

Increment the pointer.

Your Workflow section would look something like this:

In this case it is pretty straight forward as the loop runs only a predefined number of times. Thus worst case scenario; you keep doing operation on the same item.

Let's say, however, that you do not know which item you are looking for. Let's assume the conditions are so complex that you cannot resolve it with a REST call, and you have to iterate through each of the items until you find what you need. In this case you need to use the other loop; Loop with Condition. Your loop would look something like this:

It might happen though that you forget to increment the counter. Which means your cycle is doing operation on the same item over and over again, and unless the first item is the winner, you get into an infinite loop.

The Workflow Manager has a nice little feature to discover this and will give you this error message:

The workflow instance exceeded the CPU usage limit of throttle of 00:00:01.2000000 and could not be unloaded because it was not persistable.

Thus... If you see this, most probably you have some infinite loop somewhere.