In partnership with

The Tech newsletter for Engineers who want to stay ahead

Tech moves fast, but you're still playing catch-up?

That's exactly why 100K+ engineers working at Google, Meta, and Apple read The Code twice a week.

Here's what you get:

  • Curated tech news that shapes your career - Filtered from thousands of sources so you know what's coming 6 months early.

  • Practical resources you can use immediately - Real tutorials and tools that solve actual engineering problems.

  • Research papers and insights decoded - We break down complex tech so you understand what matters.

All delivered twice a week in just 2 short emails.

NodeBridge Automation Solutions

Issue 8 β€’ February 3, 2026

Preventing Infinite Loops and Runaway Workflows

⏱️ 9 min read

In This Issue

  • Why Loops Explode
  • Safeguard 1: Maximum Iteration Limits
  • Safeguard 2: Timeout Limits
  • Safeguard 3: Processed Record Tracking
  • Safeguard 4: Self-Trigger Prevention
  • Safeguard 5: Batch Processing with Checkpoints
  • Safeguard 6: Resource Monitoring
  • Testing for Loop Safety
  • Recovery: When a Loop Explodes Anyway

You built a simple loop that processes customers, updates their records, and moves on. You tested it with 10 customers and it worked fine.

You deployed it to production with 500 customers. Started fine. Then you got a Slack alert: "Workflow still running after 2 hours."

You check n8n. The loop is at iteration 47,832. Your 500 customers have somehow become 47,832 iterations. Memory is at 98%. The instance is crawling.

You kill the workflow. But what happened? And how many records did it actually update? Did it update some customers 94 times each? Did it create 47,000 duplicate records in your CRM?

You don't know, and now you get to figure it out while your production system is half-broken.

This is a loop explosion. It happens when your termination condition fails, when your data grows unexpectedly, or when triggers fire in ways you didn't anticipate. Today I'm giving you some safeguards to prevent it.

Why Loops Explode

Loops need an exit condition. When the condition fails, the loop runs forever. Here are a few common ways it happens:

1. Missing or Broken Termination Condition

Loop while items.length > 0
  Process item
  // Forgot to remove item from array

The array never shrinks. The loop never ends.

2. Data Creates More Data

For each customer:
  Get their orders
  For each order:
    Get line items
    // Customer has 1000 orders with 50 items each
    // Expected: 500 iterations
    // Actual: 500 x 1000 x 50 = 25,000,000 iterations

Your test data had 2 orders per customer. Production has customers with thousands.

3. Circular References

Process record A
  A references B β†’ Process B
  B references C β†’ Process C
  C references A β†’ Process A (again)
  // Infinite cycle

Data has relationships you didn't anticipate. The loop follows them forever.

4. Webhook Triggers Itself

Webhook receives update
  β†’ Updates record in CRM
  β†’ CRM fires webhook for update
  β†’ Workflow receives webhook
  β†’ Updates record in CRM
  β†’ ... forever

The most dangerous loop: your workflow triggers itself.

Safeguard 1: Maximum Iteration Limits

Every loop should have a hard limit, even if you don't think you need one.

Implementation:

Set iteration_count = 0
Set MAX_ITERATIONS = 1000

Loop:
  iteration_count += 1

  If iteration_count > MAX_ITERATIONS:
    Log error: "Loop exceeded maximum iterations"
    Alert: "Possible runaway loop in [Workflow]"
    Exit loop

  // Normal processing

Choosing your limit:

  • Know your expected maximum (500 customers? 10,000 orders?)
  • Set limit at 2-3x expected maximum
  • If limit is hit, something is wrong, don't just increase the limit

n8n Pattern:

Use a counter variable that increments each iteration. Add an IF node that checks if counter exceeds limit. If yes, break out of the loop and send an alert.

Safeguard 2: Timeout Limits

Sometimes iteration count isn't enough. A single iteration might hang forever.

Implementation:

Set workflow-level and node-level timeouts:

  • Workflow timeout: Maximum total execution time (e.g., 30 minutes)
  • Node timeout: Maximum time for a single operation (e.g., 60 seconds)

n8n Configuration:

Workflow Settings β†’ Execution Timeout β†’ 1800000 (30 minutes)

Individual HTTP nodes β†’ Settings β†’ Timeout β†’ 60000 (60 seconds)

What happens when timeout hits:

  • Workflow fails
  • You get notified
  • Partial data may be processed (this is why you need idempotency)

Safeguard 3: Processed Record Tracking

Prevent processing the same record twice in the same execution.

Implementation:

Set processed_ids = []

For each record:
  If record.id in processed_ids:
    Log: "Skipping already processed record"
    Continue to next

  Process record
  Add record.id to processed_ids

Why this helps:

  • Catches circular references (A β†’ B β†’ A)
  • Prevents duplicate processing if data appears multiple times
  • Creates audit trail of what was actually processed

n8n Pattern:

Use a Set node to maintain an array of processed IDs. Before processing, check if the current ID exists in the array. If yes, skip. If no, add it and continue.

Safeguard 4: Self-Trigger Prevention

The most dangerous loop is when your workflow triggers itself.

How it happens:

  1. Webhook triggers on "record updated"
  2. Workflow updates the record
  3. Update triggers the webhook
  4. Workflow runs again
  5. Infinite loop

Prevention strategies:

Strategy 1: Add a "processed" flag

If record.automation_processed == true:
  Exit (already handled by automation)

Process record
Set record.automation_processed = true

Strategy 2: Check the update source

If webhook.source == "automation":
  Exit (we triggered this ourselves)

Process record
// When updating, set source = "automation"

Strategy 3: Debounce with timestamps

If record.last_updated < 5 seconds ago:
  Exit (too recent, probably our own update)

Process record
Set record.last_updated = now

Strategy 4: Use separate trigger fields

Instead of triggering on "any update," trigger only on specific field changes that your workflow doesn't modify.

Safeguard 5: Batch Processing with Checkpoints

Don't process 10,000 records in one execution. Process in batches with checkpoints.

Implementation:

Get all records where status = "pending"
Take first 100 records (batch)

For each record in batch:
  Process record
  Set status = "processed"

If more pending records exist:
  Trigger self to process next batch
  // Or schedule next batch in 1 minute

Benefits:

  • Memory stays bounded (only 100 records in memory)
  • If something fails, you know exactly where you stopped
  • Can be paused/resumed between batches
  • Other workflows can run between batches

n8n Pattern:

Use "Split In Batches" node with batch size of 100. After processing each batch, the workflow pauses and picks up the next batch. Add a checkpoint log after each batch completes.

Safeguard 6: Resource Monitoring

Know when your loop is consuming too many resources.

What to monitor:

  • Execution time (is this taking longer than expected?)
  • Memory usage (is it growing unbounded?)
  • API calls (are you hitting rate limits?)
  • Output size (are you generating too much data?)

Implementation:

Add periodic checkpoints in your loop:

Every 100 iterations:
  Log: "Processed 100-200, memory: X, time elapsed: Y"

  If memory > threshold:
    Alert and pause

  If time > expected:
    Alert (but continue if resources are okay)

Testing for Loop Safety

Before deploying any workflow with loops:

Test 1: Empty data

  • What happens with 0 records?
  • Does the loop handle empty arrays gracefully?

Test 2: Single record

  • Does it process one record correctly?
  • Does it exit properly after one iteration?

Test 3: Expected volume

  • Test with realistic production volume
  • Monitor time and resource usage

Test 4: 10x expected volume

  • What happens with 10x normal data?
  • Does it hit your safeguards appropriately?

Test 5: Circular data

  • If your data can have relationships, test circular references
  • A β†’ B β†’ A should not cause infinite loop

Quick Wins

Actions You Can Take This Week

🟒 Beginner β€’ 15 min

Set Workflow Timeout: Go to your longest-running workflow. Open Settings β†’ Execution Timeout. Set it to 2x your expected maximum runtime (e.g., if it should take 5 minutes max, set timeout to 600000ms = 10 minutes).

🟑 Intermediate β€’ 30 min

Add Self-Trigger Prevention: Find a workflow that updates records and could potentially trigger itself. Add a check at the start: if the record was updated by automation (flag, source, or timestamp), exit immediately.

πŸ”΄ Advanced β€’ 60 min

Convert to Batch Processing: Take a workflow that processes many records in one execution. Refactor it to use Split In Batches with a batch size of 100. Add a checkpoint log after each batch. Test with production-like volume.

Next Week

Bashmatica!

A slight repurposing of the newsletter with what I think is a better name, and geared a little more toward my home-away-from-home, QA and DevOps automations and pipelines. Honestly the content won't change much at all from the past 8 issues; still covering best practices for error handling, health monitoring, and production troubleshooting, just in an upgraded format. I think you'll love it.

πŸ”§ Need help right now?

I help teams and solopreneurs debug and stabilize production automation workflows.
β€’ One-time automation audits
β€’ Fixed-fee "workflow rescue" engagements

Book a Free 15-Min Triage Call β†’

πŸ“€ If this helped you, forward it to one person running an automation workflow in production.
That's how this newsletter grows.

Until next Tuesday,

Bobby R. Goldsmith | Founder, NodeBridge Automation Solutions

P.S. The scariest loops are the ones that almost work. They process most of your data correctly, then explode on edge cases you never tested. Add iteration limits to every loop, even the simple ones. When the limit gets hit, you'll be glad you added it, and you'll learn something about your data you didn't know.

Privacy-first email. Built for real protection.

End-to-end encrypted, ad-free, and open-source. Proton Mail protects your inbox with zero data tracking.

Keep Reading