Skip to content

Fix RuntimeError: OrderedDict mutated during iteration in _fixup_self_references#348

Open
haobibo wants to merge 2 commits into
chimpler:masterfrom
haobibo:patch-1
Open

Fix RuntimeError: OrderedDict mutated during iteration in _fixup_self_references#348
haobibo wants to merge 2 commits into
chimpler:masterfrom
haobibo:patch-1

Conversation

@haobibo

@haobibo haobibo commented Jun 23, 2026

Copy link
Copy Markdown

Summary

This PR fixes a RuntimeError: OrderedDict mutated during iteration exception that occurs when resolving self-referential optional substitutions under Python 3.13+ (or when key deletion is triggered during substitution resolution).

Cause

In HOCON configs, self-referential environment overrides are commonly defined as follows:

APP_MODULE = "Aloha"
APP_MODULE = ${?APP_MODULE}

When parsing such configurations via include directives, the value history is flattened, and pyhocon tries to resolve ${?APP_MODULE} as a new optional self-reference. If the APP_MODULE environment variable is not defined, ConfigParser._fixup_self_references will substitute it with None via cls._do_substitute(substitution, None).

This operation deletes the key from the ConfigTree (which inherits from collections.OrderedDict ). Since the parser iterates directly over the ConfigTree object ( for key in config ), mutating its size by deleting the key throws RuntimeError: OrderedDict mutated during iteration (particularly strict under Python 3.13+).

Solution

Wrap the iteration in list(...) to iterate over a static snapshot of the keys:

  • Before for key in config:
  • After for key in list(config):

This decouples the dictionary iteration from any key mutations/deletions performed during the self-reference resolution phase.

@haobibo

haobibo commented Jun 23, 2026

Copy link
Copy Markdown
Author

@darthbear Could you please kindly take a review?

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.

1 participant