Skip to content

fix: keep the market-hours database when a refresh returns malformed JSON - #710

Open
pcbeingused333 wants to merge 1 commit into
QuantConnect:masterfrom
pcbeingused333:c1149-market-hours-validate
Open

pcbeingused333 wants to merge 1 commit into
QuantConnect:masterfrom
pcbeingused333:c1149-market-hours-validate

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

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 with wb+. A successful HTTP response with malformed content therefore replaced a usable database, and later MarketHoursDatabase._get_all_entries() raised JSONDecodeError.

Fix:

  • Validate the downloaded content before touching the local file, using the same parsing as MarketHoursDatabase._get_all_entries() (comments stripped, then JSON, and it must contain an entries object). 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 and os.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, missing entries, 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).

…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Keep the market-hours database when a refresh returns malformed JSON

1 participant