Loop Protection

VDX includes built-in loop safety. By default, every while, for, and for-in loop is monitored at runtime and halted when either limit is hit:

  • A single iteration takes more than 2000 ms (2 seconds) of real work
  • The loop runs more than 1,000,000 iterations (v0.1.5+)

Why?

Infinite loops (or extremely fast loops) can freeze your program, consume all CPU, and make debugging painful. VDX prevents this by default so you can write safer code without thinking about it.

How it works

Each time a loop body finishes one iteration, VDX checks how much work that iteration did and how many iterations have run. If an iteration took more than 2 seconds of real work, or the loop passes 1,000,000 iterations, the runtime throws an error:

[VDX] Loop safety: iteration took 2500ms (> 2000ms maximum).
      This loop may be infinite or too slow.
      Use @unsafe before 'while' to disable this protection.

[VDX] Loop safety: while loop exceeded 1000000 iterations.
      This loop may be infinite.
      Use @unsafe before 'while' to disable this protection.

Time blocked inside wait() and input() does not count toward the 2-second iteration budget (v0.1.5+) — only real computation is measured.

Example: infinite loop caught

class App {
    // BLOCKED after 1,000,000 iterations — even though each one is instant
    while (true) {
        // forgot to break / update a counter
    }
}

Example: safe loops

class App {
    // Fine — fast iterations, well under the cap
    let i = 0;
    while (i < 5) {
        print(i);
        i = i + 1;
    }

    // Fine — waiting doesn't count toward the 2s iteration budget (v0.1.5+)
    let n = 0;
    while (n < 5) {
        wait(2500);   // blocked time is excluded
        n = n + 1;
    }
}

Bypassing with @unsafe

If you know what you are doing and need a fast loop, use the @unsafe annotation. See the @unsafe documentation for details.