Lightning-AI / Lightning-AI/lightning-thunder

Error "dictionary changed size during iteration" in interpreter

Open
#2,095 1 comment 0 reactions 1 assignee View on GitHub

@t-vi is already working on this.

Since May 19, 2025.

interpreter
Dominant language
Python
Stars
1.5k
Forks
121
PR merge metrics
No merged PRs in 30d

Description

this looks like a race condition in interpretation

    def populate_item_wrappers(l):
        ctx: InterpreterCompileCtx = get_interpretercompilectx()
        if not ctx._with_provenance_tracking:
            return
    
        assert isinstance(l, WrappedValue)
        # to do: generalize
        if wrapped_isinstance(l, (list, tuple)):
            if l.item_wrappers is None:
                l.item_wrappers = [None for _ in range(len(l.value))]
            assert isinstance(l.item_wrappers, list)
            assert len(l.value) == len(l.item_wrappers), f"{len(l.value)=} {len(l.item_wrappers)=}"
    
            for i, v in enumerate(l.value):
                if l.item_wrappers[i] is None:
                    wv = wrap_binary_subscr(v, l, i)
                    l.item_wrappers[i] = wv
            return
    
        if wrapped_isinstance(l, dict):
            assert isinstance(l.item_wrappers, dict)
>           for k, v in l.value.items():
E           RuntimeError: dictionary changed size during iteration

seems to appear as

FAILED thunder/tests/test_jit_general.py::test_modulelist_idx - thunder.core.interpreter.InterpreterError: Encountered exception RuntimeError: dictionary changed size during iteration while tracing MyModel(
  (l): ModuleList(
    (0-1): 2 x Linear(in_features=4, out_features=4, bias=True)
  )
):

not always but more than never

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.