Skip to content

"Dictionary changed size during iteration" in 2.16.0 #2489

@timoffex

Description

@timoffex

In metaflow==2.16.0, the code here introduced in #2487 can raise a RuntimeError because sys.modules may be modified while being iterated:

Traceback (most recent call last):
  File ".../site-packages/metaflow/cli.py", line 646, in main
    start(auto_envvar_prefix="METAFLOW", obj=state)
  ...
  File ".../site-packages/metaflow/cli_components/run_cmds.py", line 63, in before_run
    obj.package = MetaflowPackage(
  File ".../site-packages/metaflow/package/__init__.py", line 87, in __init__
    self._mfcontent = MetaflowCodeContentV1(criteria=_module_selector)
  File ".../site-packages/metaflow/packaging_sys/v1.py", line 46, in __init__
    modules = sorted(modules, key=lambda x: x[0])
RuntimeError: dictionary changed size during iteration

In my case, this happened because of the hasattr check in the criteria callback, which ended up calling a module's __getattr__ which performed an import. To repro, just use the helloworld.py example and add import beartype at the top.

It's common to import stuff dynamically in Python, so iterating sys.modules while calling any code isn't safe. The code should probably make a copy.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions