Skip to content

fix: keep the modules cache when the downloaded JSON is malformed - #708

Open
pcbeingused333 wants to merge 1 commit into
QuantConnect:masterfrom
pcbeingused333:fix/modules-json-keep-cache
Open

pcbeingused333 wants to merge 1 commit into
QuantConnect:masterfrom
pcbeingused333:fix/modules-json-keep-cache

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

Fixes #707

_download_modules_json() opened the modules cache with mode w before evaluating res.json(). If the CDN answers HTTP 200 with a malformed body, opening the file already truncates the usable cache and then the decoding raises. At startup that exception is swallowed, the fallback loads the now-empty cache, and the CLI fails to start instead of keeping the previous modules data.

Fix: decode the response first, write the JSON to a temporary file next to the cache, and replace the cache only after that write succeeds. A malformed response or a failed write now leaves the existing cache as it was, and the temporary file is removed in every case.

Tests: tests/models/test_download_modules_json.py covers a malformed 200 response with a pre-existing cache (the cache content must be unchanged and no temporary file left) and a valid response replacing the cache. The first test fails without the change (the cache is empty afterwards) and passes with it; tests/models and tests/components/util/test_update_manager.py pass (141 tests).

This change was prepared with the help of an AI assistant (Claude), and the submitter is responsible for it.

_download_modules_json opened the cache with mode w before decoding the response, so an HTTP 200 with a malformed body truncated the cache and the CLI could not start. Decode first and write the replacement to a temporary file that replaces the cache only once complete.

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.

Malformed modules JSON response destroys the existing cache

1 participant