Fix minion crash when grains config option is empty - #69891
Merged
dwoz merged 3 commits intoAug 11, 2026
Merged
Conversation
BrianHa94
force-pushed
the
fix-61321-minion-crash-on-invalid-empty-grains
branch
from
July 28, 2026 05:43
378f500 to
3bb5529
Compare
twangboy
requested changes
Jul 28, 2026
dwoz
force-pushed
the
fix-61321-minion-crash-on-invalid-empty-grains
branch
from
August 9, 2026 02:27
57f58c5 to
27d07da
Compare
dwoz
force-pushed
the
fix-61321-minion-crash-on-invalid-empty-grains
branch
from
August 9, 2026 02:30
27d07da to
20e9d04
Compare
dwoz
reviewed
Aug 9, 2026
Contributor
There was a problem hiding this comment.
not isinstance(opts["grains"], dict) -- catches empty and wrong-type in one guard. No valid grains is anything but a mapping. Drop the warning; document the shape.
conf/minion above #grains::
+#
+# The value of 'grains' must be a mapping. Use 'grains: {}' for an
+# explicit empty section. Any non-dict value is silently defaulted
+# to an empty dict.
#grains:doc/ref/configuration/minion.rst inside the grains entry, after the example:
+The value of ``grains`` must be a mapping. Use ``grains: {}`` for an
+explicit empty section. Any non-dict value is silently defaulted to
+an empty dict.
+
.. conf_minion:: grains_blacklistTests: parametrize over ["", '""', "[]", "foo", "42", "[1, 2]"] (all defaulting to {}) in tests/unit/test_config.py + tests/pytests/unit/loader/test_loader.py; same for tests/pytests/integration/cli/test_salt_minion.py with [None, "", [], "foo", 42, [1, 2]], drop the log-file assertions and the import salt.utils.files.
An empty 'grains:' config option parses to None instead of a dict,
which crashed the minion during startup with "TypeError: 'NoneType'
object is not iterable" when the loader tried to build the
__grains__ NamespacedDictWrapper.
Default the option to an empty dict in both places that read it:
apply_minion_config, and salt.loader.grains(), which independently
re-reads the raw config file off disk. Log a warning in each case
pointing out that 'grains: {}' should be used instead.
Fixes saltstack#61321
Only warn/default the 'grains' config option when it is explicitly
present and None, not merely absent from opts. The looser
opts.get("grains") is None check could not distinguish "grains: set
to empty" from "grains key never set at all" (e.g. a sparse defaults
dict passed into apply_minion_config without a 'grains' key), causing
a false-positive warning in that case. This matches the equivalent
check already used in salt.loader.grains().
Also move log_file = factory.config["log_file"] in the new
integration test out of the try block, since it doesn't depend on the
test.ping call succeeding and reads more clearly next to the log file
assertions it's used for.
Broaden the empty-grains guards in apply_minion_config and salt.loader.grains() from an explicit None check to isinstance(value, dict), so any non-mapping value (empty string, list, scalar, ...) is defaulted to an empty dict, not just an explicitly empty 'grains:' key. Drop the runtime warning in favor of documenting the required shape in conf/minion and doc/ref/configuration/minion.rst. Parametrize the existing tests over a range of non-dict grains values instead of just None, and drop the now-irrelevant log/warning assertions.
twangboy
approved these changes
Aug 11, 2026
dwoz
approved these changes
Aug 11, 2026
|
Congratulations on your first PR being merged! 🎉 |
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?
Fixes a minion crash on startup caused by a
grains:config option that isn't a mapping. YAML parsesgrains:with no value asNone, and non-dict values like a string, number, or list are also possible — two separate code paths (apply_minion_configandsalt.loader.grains(), which independently re-reads the raw config file off disk) assumed the option was always a dict. That crashed the minion withTypeError: 'NoneType' object is not iterablewhen building the__grains__context wrapper.Both locations now check
isinstance(value, dict)and silently default the option to an empty dict ({}) for any non-dict value, not justNone. The required shape ofgrainsis documented inconf/minionanddoc/ref/configuration/minion.rstrather than surfaced as a runtime warning.What issues does this PR fix or reference?
Fixes #61321
Previous Behavior
A minion configured with a
grains:config option that wasn't a mapping (e.g.grains:with no value, an empty string, or a scalar) would crash on startup with:raised from
salt.utils.context.NamespacedDictWrapper.__init__via the loader's__prep_mod_opts.New Behavior
The minion starts normally. Any non-dict
grainsvalue is silently treated as{}. The required shape (grainsmust be a mapping; usegrains: {}for an explicit empty section) is documented in the minion configuration reference and the example minion config file.Merge requirements satisfied?
Unit tests in
tests/pytests/unit/test_config.pyandtests/pytests/unit/loader/test_loader.py, plus an integration test intests/pytests/integration/cli/test_salt_minion.py, all parametrized over a range of non-dictgrainsvalues (None, empty string, empty list, string, number, list).Commits signed with GPG?
No