Infinite loop over items x2

Hello there,

I’m experiencing an infinite loop in my workflow and would really appreciate your insights.

:hammer_and_wrench:Context:

  • The workflow starts by listing multiple .json files from a Google Drive folder.
  • Each file contains an object with a string key and an array of sub-objects (let’s call them entries).
  • I loop through the files one by one using SplitInBatches.
  • For each file:
    • I download and parse it.
    • Then I use a Code node to split the array inside the JSON into individual items.
    • These items are then supposed to be processed one by one in another loop.

Problem:

As soon as the splitting happens in the Code node, the number of executions explodes.

  • The first batch is processed normally.
  • But right after that, thousands of executions start running — even though there are only a few dozen JSON files in total.
  • It’s as if the newly generated items were being re-injected into the main file-processing loop, causing an unintended recursive loop.

Thanks for your advice.

Share the output returned by the last node

Information on your n8n setup

  • **n8n version: 1.9
  • Database (default: SQLite):
  • Running n8n via (Docker):

Hey @Froldev

Gut feeling says the issue has to do with item linking and generally that nested loops don’t behave exactly as you’d expect compared to code.

Item linking

Try returning only one item on the return path from the second loop node to the first loop node. Usually when you return more than the batch size, the loop node will treat the unexpected new items and it’ll try to loop again. A solution is to use the aggregate node in between them to ensure only 1 item is returned.

See docs here - item linking

Use a Subworkflow Instead!

Alternatively, consider using a subworkflow (“Execute another workflow” node) instead of a second loop. Separating the executions avoids most item linking issues but again, ensure you’re not returning more items than necessary.

2 Likes

Nice idea, I agree with this

Thank Jim, I understand better the item linking now and the subworkflow works like a charm in that situation !
Best

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.