azureazure-logic-appsazure-eventhub

Variable memory leak with Azure Logic App Send to Hub action


I use an Azure logic app to collect data and send messages to an eventhub. I have observed that the "send event" action has a memory leak when the action is in a for each loop.

Logic App Designer

In each iteration of the loop, a unique value of the variable alert_formatted is set and then that variable is sent as the message body to event hub. For a run with 8 iterations of the loop, I've verified from the run history that each iteration has a unique value of alert_formatted, however, in two of the iterations, the actual base64 string sent to the event hub was a duplicate of a prior iteration.

I added a 1-second delay action between setting the variable and sending the event, which had the curious outcome of all events being sent to the event hub to be duplicates of each other. What I believe this means is that, at least for the event hub send event action, there is a single global instance of a variable. And if that's true, then what I'm observing are periodic duplicate events in event hub caused by a race condition where the value of my variable is changing mid-processing of an individual iteration of my for each loop.

I've lowered the for each loop concurrency value to 1, which has eliminated my duplicate problem, but also increased the execution cost of my logic app.


Solution

  • There is no memory leak. The behaviour you are describing is in line with the documentation:

    By default, the iterations in a For each loop run at the same time in parallel. However, when you have nested loops or variables inside the loops where you expect predictable results, you must run those loops one at a time or sequentially.

    If each new value of the alert_formatted variable is completely independent from the previous values, then you can either replace the Set variable action with the Compose action, or just use an expression function directly in the Content field of your Send event action. This will allow you to run the loops in parallel without side effects.

    If you do need to use a variable then the loops must be run sequentially.