← Back to Docs

Loop Protection

VDX includes built-in loop safety. By default, every while loop is monitored at runtime. If a single iteration completes in less than 2000 milliseconds (2 seconds), the loop is immediately halted with an error.

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 while loop body finishes one iteration, VDX checks how long that iteration took. If it was under 2 seconds, the runtime throws an error:

[VDX] Loop safety: iteration completed in 0ms (< 2000ms minimum).
      This loop may be infinite or too fast.
      Use @unsafe before 'while' to disable this protection:
      @unsafe while (condition) { ... }

Example: blocked loop

class App {
    let i = 0;
    // This will be BLOCKED — each iteration is instant (< 2s)
    while (i < 100) {
        print(i);
        i = i + 1;
    }
}

Example: safe loop with wait

class App {
    let i = 0;
    // This is fine — wait(2100) makes each iteration > 2s
    while (i < 5) {
        print(i);
        i = i + 1;
        wait(2100);
    }
}

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.