fix: recover from corrupted network-config.json cache - #7079
Open
pujitha24 wants to merge 1 commit into
Open
Conversation
Init._write_network_config_json() read the cached network-config.json instance file via util.load_json() with no exception handling. A corrupted, empty, or truncated cache file (e.g. from an interrupted write) makes json.loads() raise JSONDecodeError, or makes util.load_json()'s root-type check raise TypeError when the parsed value isn't a dict. Either exception was unhandled and propagated out of _write_network_config_json(), which is called from apply_network_config() during boot, so a bad cache file broke network configuration entirely instead of being safely regenerated. Wrap the load in a try/except covering JSONDecodeError, TypeError, and ValueError, matching the existing pattern used for the same util.load_json() call a few lines below in this file. On failure, log a warning and treat the cache as absent so the current network config is written out fresh, mirroring the no-cache-file code path just below. Validation: added test_write_network_config_json_recovers_from_corrupted_cache to tests/unittests/test_stages.py, parametrized over malformed JSON, an empty file, and syntactically valid JSON of the wrong type (e.g. a list). Confirmed the new test fails with an unhandled JSONDecodeError on the pre-fix code and passes after the fix. Ran `python -m pytest tests/unittests/test_stages.py -q`: 60 passed. Also ran black, isort, ruff, `pylint -E`, and mypy on the changed files; all clean. Report: canonical#7069 Signed-off-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com> Assisted-by: claude-sonnet-5 (via Claude Code)
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.
Proposed Commit Message
Additional Context
The root cause and fix were suggested in the issue itself. This PR
additionally catches
TypeError(not justJSONDecodeError/ValueError), sinceutil.load_json()raisesTypeErrorwhen thecached file contains syntactically valid JSON that isn't a dict
(e.g. a truncated write leaving
nullor a list) -- the sameexception class already handled for an equivalent
util.load_json()call a few lines below in
cloudinit/stages.py(system_info/featurescache) and in
cloudinit/sources/DataSourceVMware.py.Test Steps
Ran the affected test module directly:
60 passed.
The new test
test_write_network_config_json_recovers_from_corrupted_cacheisparametrized over three corrupted-cache shapes (malformed JSON, an
empty file, and valid JSON of the wrong type) and asserts that
_write_network_config_jsonlogs a warning and rewrites the cacheinstead of raising. I confirmed this test fails with an unhandled
json.decoder.JSONDecodeErroragainst the pre-fix code and passesafter the fix.
Also ran
black --check,isort --check-only,ruff check,pylint --disable=all --enable=E, andmypyagainst the twochanged files (
cloudinit/stages.py,tests/unittests/test_stages.py); all clean.This change was validated with local unit tests and static checks
only (no live/dev-stack boot was performed), since the defect is a
straightforward unhandled-exception bug reproducible with a targeted
unit test.
Merge type
Fixes #7069