fix to decode JSON from response to get_facts#826
Open
chidanandpujar wants to merge 1 commit into
Open
Conversation
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.
ROOT CAUSE & FIX SUMMARY
ROOT CAUSE
Ansible persistent connections use a JSON-RPC Unix socket. The connection plugin
process serializes return values via pickle.dumps(). When get_facts() returned
dict(self.dev.facts), the dict contained PyEZ junos.version_info namedtuple
objects which are not safely picklable. This caused the connection process to
crash without sending a socket reply. The caller received None from the socket,
which json.loads('None') could not parse, producing:
"Unable to decode JSON from response to get_facts(). Received 'None'."
Secondary bug: PR #816 partially fixed this using json.dumps(..., default=str),
but str() converted namedtuples to opaque strings like
"junos.version_info(major=(25, 4), type=R, minor=1, build=12)" instead of
structured dicts, breaking version_info and junos_info[re0]['object'].
FILES MODIFIED
plugins/connection/pyez.py
a) Added module-level helper before class Connection:
def _serialize_fact(obj):
if hasattr(obj, "_fields"): # namedtuple
return dict(zip(obj._fields, obj))
return str(obj)
Converts junos.version_info namedtuples to plain dicts
e.g. {"major": [25, 4], "type": "R", "minor": 1, "build": 12}
Falls back to str() only for truly unknown types.
b) Updated get_facts() method:
@ensure_connect # guarantees device is connected
def get_facts(self):
facts = self.dev.facts
if facts is None: # guard against None facts
return {}
return json.loads(json.dumps(dict(facts), default=_serialize_fact))
Changes: added @ensure_connect, None guard, default=_serialize_fact
(was: default=str, no decorator, no None guard)
plugins/modules/facts.py
Added None guard in get_facts_dict() pyez path:
else:
facts = junos_module.get_facts()
if facts is None: # <-- added
facts = {} # <-- added
Prevents KeyError/TypeError if get_facts() returns None.
Before fix:
After fix: