While Loop Close
The recursion engine inside kijai's loop nodes
- flow_control
- initial_value0
- initial_value1
- initial_value2
- initial_value3
- initial_value4
- value0
- value1
- value2
- value3
- value4
Every node in this pack is scaffolding around one hard problem: ComfyUI has no loops. A node gets its inputs, runs once, done. _WhileLoopClose is where kijai's solution to that problem actually lives - it's the recursion engine that TensorLoopClose delegates to. Dev-only and hidden from the menu, but it's the most technically interesting node in the repo, so it's worth a peek under the hood.
What it does
It's the "foot" of a while loop. It takes flow_control (pointing back at the matching _WhileLoopOpen), a condition boolean, and the five loop-carried values. If condition is false, it's done - it returns the current values and the loop exits. If condition is true, it doesn't just say "go again." It rebuilds the loop body and runs it again with the new values, right now, inside the same execution.
That rebuild is the clever part. ComfyUI's dynamic prompt system can expand a node into a subgraph at execution time, and _WhileLoopClose leans on it hard:
- It walks the dependency graph (
_explore_dependencies) to find every node inside the loop - everything downstream of the Open node and upstream of itself. - It clones all of those nodes into a fresh graph, reusing the "Recurse" name for its own clone so the graph names don't explode exponentially across iterations.
- It wires the five carried values into the new Open node's
initial_valueslots and expands again.
That's a while loop by graph rewriting. Each iteration literally builds and executes the body from scratch with fresh state. It's the same trick ComfyUI's own expansion uses, and it's why the pack can loop without a scheduler hitching a ride on your workflow.
Inputs and outputs
- flow_control - from the matching Open node.
- condition (boolean) - false exits the loop, true re-runs it.
- initial_value0 … initial_value4 (optional) - carried state, fed to the next iteration's Open node.
- Outputs: value0 … value4 - the final carried values once the loop terminates.
In the TensorLoop pair, the "carried state" is a single packed dict - the accumulated frames, the remaining counter, the open node id, the frame target - so only initial_value0 is actually used.
Install and the honest caveats
Install is pack-standard:
cd ComfyUI/custom_nodes
git clone https://github.com/kijai/ComfyUI-NativeLooping_testing
Restart ComfyUI, or use Manager ("NativeLooping"). No extra dependencies beyond a recent ComfyUI.
Three honest warnings. First: recursion means each iteration is a fresh graph execution, and GraphBuilder expansion can get slow or memory-heavy if the loop body is enormous or the iteration count is in the hundreds - this is the price of loop-by-expansion. Second: this is a _testing repo from kijai, explicitly a candidate for native loop nodes in ComfyUI core, so expect the internals to change without notice. Third: the condition must eventually turn false or you get an infinite loop - which is exactly why the TensorLoop pair disables total_frames mode when you're not accumulating. If a workflow using this pack ever hangs, that's the first thing to check.
Inputs (7)
| Name | Type | Default | Description |
|---|---|---|---|
| flow_control | FLOW_CONTROL | — | |
| condition | BOOLEAN | — | |
| initial_value0opt | * | — | |
| initial_value1opt | * | — | |
| initial_value2opt | * | — | |
| initial_value3opt | * | — | |
| initial_value4opt | * | — |
Outputs (5)
| Name | Type | Description |
|---|---|---|
| value0 | * | — |
| value1 | * | — |
| value2 | * | — |
| value3 | * | — |
| value4 | * | — |