Skip to content

Fix grains.append leaking defaultdict into persisted grain state (#64017) - #69648

Merged
dwoz merged 1 commit into
saltstack:3006.xfrom
dwoz:fix/64017-grains-defaultdict-leak
Jul 9, 2026
Merged

dwoz merged 1 commit into
saltstack:3006.xfrom
dwoz:fix/64017-grains-defaultdict-leak

Conversation

@dwoz

@dwoz dwoz commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Prevents grains.append (and by extension grains.list_present) from persisting a
collections.defaultdict into the minion's grain state. That leaked defaultdict caused
subsequent lookups to silently auto-materialize empty children instead of raising
KeyError, so sibling nested grains.list_present calls under a shared parent path
failed 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_list semantics are unchanged.

Previous Behavior

Applying two grains.list_present states with keys under a common nested parent -- e.g.

core-services:monitored:
  grains.list_present:
    - value: basic
core-services:mon-config:rules:
  grains.list_present:
    - value: rules1

resulted in the second state failing with:

Failed append value rules1 to grain core-services:mon-config:rules

because the first list_present had left an _infinitedict (a self-nesting
collections.defaultdict) at __grains__["core-services"]. traverse_dict_and_list
walked into it, and defaultdict.__getitem__ silently inserted empty children --
so grains.append was handed an empty defaultdict where it expected [].

New Behavior

Both states succeed. __grains__["core-services"] and its children are plain
dict instances after either call, so traverse_dict_and_list raises KeyError
for absent paths and callers see the correct default (or []).

Merge requirements satisfied?

  • Docs (no user-facing docs affected; the buggy behavior was never documented)
  • Changelog (changelog/64017.fixed.md)
  • Tests written/updated (test_list_present_multiple_nested_siblings_64017)

Commits signed with GPG?

Yes

@dwoz
dwoz requested a review from a team as a code owner July 2, 2026 06:15
@dwoz dwoz added this to the Sulphur v3006.26 milestone Jul 2, 2026
@dwoz dwoz added the test:full Run the full test suite label Jul 2, 2026
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

This branch was previously deployed

1 inactive deployment
ci — fb500ba2 Deployed Jul 3, 2026 by dwoz via Build Onedir Packages / Windows (x86) #25153
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test:full Run the full test suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants