Repository navigation
Fix grains.append leaking defaultdict into persisted grain state (#64017) - #69648
Merged
Merged
Conversation
grains.append walked up the delimited-key hierarchy calling
`get(key, _infinitedict(), delimiter)`. When the parent path did not
yet exist, `get()` returned that fresh `collections.defaultdict`, which
was then mutated in-place and persisted through `setval`. The next
`grains.get` on any sibling path under the same parent traversed
through the defaultdict; `salt.utils.data.traverse_dict_and_list`
relies on `KeyError` from `ptr[each]` to signal a missing key, but a
`defaultdict(_infinitedict)` silently auto-inserts an empty child
instead. Callers such as `grains.list_present` therefore received an
empty defaultdict where they expected `[]`, rejected it as "not a
valid list", and failed with "Failed append value X to grain Y".
Use a plain `{}` as the default. Auto-nesting is never needed there:
the loop writes one level at a time via `.update({rest: grains})`, and
every parent along the path is written in its own iteration.
Fixes saltstack#64017
twangboy
approved these changes
Jul 9, 2026
This branch was previously deployed
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.
What does this PR do?
Prevents
grains.append(and by extensiongrains.list_present) from persisting acollections.defaultdictinto the minion's grain state. That leaked defaultdict causedsubsequent lookups to silently auto-materialize empty children instead of raising
KeyError, so sibling nestedgrains.list_presentcalls under a shared parent pathfailed with "not a valid list".
What issues does this PR fix or reference?
Fixes #64017
Related history: #47912 (original umbrella report) and closed-for-inactivity #64018
(reporter's traverse-layer patch). This PR takes the narrower grains-layer approach
so
salt.utils.data.traverse_dict_and_listsemantics are unchanged.Previous Behavior
Applying two
grains.list_presentstates with keys under a common nested parent -- e.g.resulted in the second state failing with:
because the first
list_presenthad left an_infinitedict(a self-nestingcollections.defaultdict) at__grains__["core-services"].traverse_dict_and_listwalked into it, and
defaultdict.__getitem__silently inserted empty children --so
grains.appendwas handed an emptydefaultdictwhere it expected[].New Behavior
Both states succeed.
__grains__["core-services"]and its children are plaindictinstances after either call, sotraverse_dict_and_listraisesKeyErrorfor absent paths and callers see the correct
default(or[]).Merge requirements satisfied?
changelog/64017.fixed.md)test_list_present_multiple_nested_siblings_64017)Commits signed with GPG?
Yes