Node loose access to previous variables inside a loop

Describe the problem/error/question

I’m facing a weird issue since we updated our n8n version.

We have a sub flow execution inside a loop. The parameters for this sub flow are pulled from an “Edit Fields (Set)” node that is outside of the loop, so the variables are set before starting the loop.
Before we updated it worked great, all executions were able to run.
Now, at around loop number 350, the variables from the “Edit Fields (Set)” node seems to disapear, or are not available anymore.

You can see in my screenshot for example the variable “fieldsList” is set correctly with a value in the “Set” node, but the subworkflow node doesn’t see the value (“undefined”):

And this occurs only after some loops (around 350), all the previous loops worked fine

What is the error message (if any)?

Please share your workflow

Share the output returned by the last node

Information on your n8n setup

  • n8n version: 1.108.1
  • Database (default: SQLite): ?? (I’m using n8n cloud)
  • n8n EXECUTIONS_PROCESS setting (default: own, main): ??
  • Running n8n via (Docker, npm, n8n cloud, desktop app): n8n cloud
  • Operating system: Windows

@Thomas_Crenn Looks like the issue is coming from how the data is passed around. primaryKey works because it’s a simple value, but fieldsList is an array and n8n doesn’t always keep that alive when you reference it from an old node after hundreds of loop runs. That’s why you’re seeing undefined at some point.

The safer way is to not “look back” at the Set Object Variables node every time. Instead, add fieldsList to the item before the loop starts, so it travels with each iteration. Then in your subworkflow you can just use:

{{$json.fieldsList}}

This way, the data is part of the current item and won’t disappear no matter how many times the loop runs. It also avoids adding a Set node inside every single iteration, you only need to attach it once before the loop.

In short : Add a Set node right before the loop starts (e.g. before “Set Loop Variables”). In that Set, map in fieldsList from your “Set Object Variables” node.

I don’t think this is the issue, we’re having the same type of issue on our solution.
We are using the Pro cloud plan with engine version 1.108.2.
Here is an example flow I’ve created, which suppose to run 1000 times in the loop, on my tests it stops after 801 iterations because the node ‘Static json’ (Set node) doesn’t return value at this iteration.

Having the same issue here, @amid @Thomas_Crenn did you find a solution?

I think I fixed the issue by making sure to not use references that are not in the input of the current node (output of the previous node). So I make sure to propagate the viariables throught all nodes in the loop so that I can just use $json