Repository navigation
fix: keep the market-hours database when a refresh returns malformed JSON - #710
Open
pcbeingused333 wants to merge 1 commit into
Open
pcbeingused333 wants to merge 1 commit into
pcbeingused333 wants to merge 1 commit into
Conversation
…JSON update_database_files() wrote the downloaded market-hours database bytes straight over the local file, so an HTTP 200 with malformed content replaced valid data. Validate the content with the same parsing MarketHoursDatabase uses before writing, and write through a temporary file that is moved into place (keeping the existing file's mode). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
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.
Fixes #709
Root cause:
DataDownloader.update_database_files()passed the downloaded market-hours database bytes straight to_store_local_file(), which opened the existing file withwb+. A successful HTTP response with malformed content therefore replaced a usable database, and laterMarketHoursDatabase._get_all_entries()raisedJSONDecodeError.Fix:
MarketHoursDatabase._get_all_entries()(comments stripped, then JSON, and it must contain anentriesobject). On failure the existing file is kept and the error is logged through the existing generic handler._store_local_file()now writes to a temporary file in the same directory andos.replace()s it into place, copying the existing file's mode, so an interrupted write cannot truncate the database either.The refresh timestamp is still recorded where it was; that behaviour is unchanged to keep this PR small.
Tests: new
tests/components/cloud/test_data_downloader.py(malformed response, missingentries, valid commented response replaces the file, no temp files left behind): red 2 failed -> green 5 passed together with the existing cloud tests.This change was prepared with an AI coding assistant (Claude).