Skip to content

Fix NamedLoaderContext container-protocol when value() is None (#65504) - #70923

Open
dwoz wants to merge 1 commit into
saltstack:3008.xfrom
dwoz:dwoz/fix/65504-namedloader-dict-default
Open

dwoz wants to merge 1 commit into
saltstack:3008.xfrom
dwoz:dwoz/fix/65504-namedloader-dict-default

Conversation

@dwoz

@dwoz dwoz commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Summary

Supersedes #69644. Resolves #65504.

salt '*' config.items and similar minion jobs crashed on the master with AttributeError: 'NoneType' object has no attribute '__iter__' when the returned payload contained a NamedLoaderContext whose value() returned None. salt.payload.dumps msgpack-encodes any collections.abc.MutableMapping via dict(obj), which iterates via __iter__ -> self.value().__iter__().

Why this fix lives in salt/loader/dunder.py and not in the dunders themselves

The mapping-typed dunders (__opts__, __context__, __pillar__, __grains__) now pass default={} to named_context(). value() returns an empty mapping off-context, honoring the MutableMapping contract that NamedLoaderContext advertises.

Reviewers of #64055 (April 2023) deliberately removed an earlier NamedLoaderContext branch in salt.payload.dumps and directed the fix to the loader layer so dunder globals would pass .value() outside execution modules rather than special-casing the serializer. This change respects that direction.

__file_client__ stays default=None: it is not a mapping, and None is its documented "no file client" sentinel.

Previous iteration

#69644 took a different approach — guarding the seven container-protocol dunders on NamedLoaderContext itself (__iter__, __len__, __contains__, __getitem__, __setitem__, __delitem__, get). That works but introduces semantics inconsistent with dict (notably __setitem__ raising TypeError instead of creating an entry) and silently masks the real-bug signal of "__opts__ was None where it shouldn't be." This 4-line construction-site fix keeps the invariant that NamedLoaderContext always backs a real mapping.

Test plan

  • test_dunder_mapping_globals_default_to_empty_dict — strict regression: __opts__/__context__/__pillar__/__grains__ have default == {}, __file_client__ has default is None. Fails without the fix.
  • test_dunder_opts_round_trips_through_payload_off_context — load-bearing scenario: salt.payload.dumps on a payload containing salt.loader.dunder.__opts__ off-context round-trips to {"ctx": {}}. Fails without the fix with AttributeError: 'NoneType' object has no attribute '__iter__'.
  • Existing 10 tests in tests/pytests/unit/loader/test_context.py still pass.

…tack#65504)

salt '*' config.items and similar minion jobs crashed on the master
with AttributeError: 'NoneType' object has no attribute '__iter__'
when a NamedLoaderContext whose value() returned None was included
in the returned payload.  salt.payload.dumps msgpack-encodes any
collections.abc.MutableMapping via dict(obj), which iterates via
__iter__ -> self.value().__iter__().  Off-context, the four mapping-
typed dunders fell back to the construction default of None and
crashed at that dict() call.

Fix at the dunder construction site in salt.loader.dunder: pass
default={} for the mapping-typed dunders (__opts__, __context__,
__pillar__, __grains__).  value() now returns an empty mapping
off-context, honoring the MutableMapping contract the class
advertises.  __file_client__ stays default=None: it is not a
mapping, and None is its documented 'no file client' sentinel.

Reviewers of saltstack#64055 (April 2023) deliberately removed the earlier
NamedLoaderContext branch in salt.payload.dumps and directed the
fix to the loader layer; this change respects that direction.
@dwoz
dwoz force-pushed the dwoz/fix/65504-namedloader-dict-default branch from 7ebc751 to 920650f Compare November 23, 2026 23:46

This branch was previously deployed

1 inactive deployment
ci — 920650f7 Deployed Nov 24, 2026 by dwoz via Build Onedir Packages / macOS (arm64) #29567
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.

[BUG] Timeout while running config.items

2 participants