Fix RuntimeError: OrderedDict mutated during iteration in _fixup_self_references#348
Open
haobibo wants to merge 2 commits into
Open
Fix RuntimeError: OrderedDict mutated during iteration in _fixup_self_references#348haobibo wants to merge 2 commits into
haobibo wants to merge 2 commits into
Conversation
Author
|
@darthbear Could you please kindly take a review? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes a
RuntimeError: OrderedDict mutated during iterationexception 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:
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:
for key in config:for key in list(config):This decouples the dictionary iteration from any key mutations/deletions performed during the self-reference resolution phase.