Skip to content

fix: prevent infinite loop in handle_metadata_vars when variable references cycle#196

Merged
patrickchugh merged 1 commit into
patrickchugh:mainfrom
jespo2021:fix/handle-metadata-vars-infinite-loop
May 20, 2026
Merged

fix: prevent infinite loop in handle_metadata_vars when variable references cycle#196
patrickchugh merged 1 commit into
patrickchugh:mainfrom
jespo2021:fix/handle-metadata-vars-infinite-loop

Conversation

@jespo2021
Copy link
Copy Markdown
Contributor

What

handle_metadata_vars hangs indefinitely when find_replace_values produces a cycling sequence of values (e.g. local.Alocal.Blocal.A → ...).

The existing guard only breaks when value == old_value (a fixed point). Cycles are different: the value changes each iteration and never repeats consecutively, so the break is never reached.

Root cause

while ("local." in value or "var." in value or ...):
    old_value = value
    value = find_replace_values(value, mod, tfdata)
    if value == old_value:  # only catches fixed points, not cycles
        break

If find_replace_values expands local.A to something containing local.B, then local.B back to something containing local.A, the loop runs forever with no output.

Fix

Add a _seen_values set that tracks every value encountered in the current resolution pass. If the same value appears again, we've detected a cycle — emit the existing "unresolved references remain" warning and break.

_seen_values: set = set()
while (...):
    if value in _seen_values:
        # cycle detected
        click.echo(WARNING...)
        break
    _seen_values.add(value)
    ...

How I found it

Running terravision draw against a Terraform project that uses a Cognito module with a local.lambda_arn map. The module defines optional lambda ARN locals that are conditionally populated — when the lambda isn't configured, the reference chain cycles instead of resolving to a terminal value. Terravision printed all the "Processing resources" entries, then hung silently forever with 100% CPU.

Tested

Confirmed fix resolves the hang on the reproducing project. The diagram generates successfully after the patch.

…a_vars

The while loop in handle_metadata_vars only broke when value == old_value
(a fixed point). When find_replace_values produces an oscillating sequence
(e.g. local.A → local.B → local.A → ...), value changes each iteration
but never resolves — causing an infinite hang.

Add a _seen_values set to detect cycles. If the current value has already
been visited in this resolution pass, emit the existing "unresolved
references remain" warning and break immediately.

Reproduced with a Cognito module that defines a local.lambda_arn map —
its unresolvable references caused terravision to hang indefinitely after
printing the "Processing resources" step, with no timeout or error output.
@patrickchugh patrickchugh merged commit 534bf50 into patrickchugh:main May 20, 2026
1 check passed
@patrickchugh
Copy link
Copy Markdown
Owner

@jespo2021 Grateful for the PR. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants