Skip to content

Recover safely if the add-on store cache is invalid#16461

Merged
seanbudd merged 3 commits into
nvaccess:betafrom
nvdaes:fixBadCachedAddons
May 10, 2024
Merged

Recover safely if the add-on store cache is invalid#16461
seanbudd merged 3 commits into
nvaccess:betafrom
nvdaes:fixBadCachedAddons

Conversation

@nvdaes

@nvdaes nvdaes commented Apr 29, 2024

Copy link
Copy Markdown
Collaborator
  • Don't restart if cache files in add-on store are not correct
  • Update changelog

Link to issue number:

Fixes #16362

Summary of the issue:

When cache files of the add-on store contain invalid data, NVDA is restarted.

Description of user facing changes

None

Description of development approach

In the _getCachedAddonData function of the _DataManager class, a cachedAddonDatavariable will try to get the value of the _createStoreCollectionFromJson(data)`.
So, if data doesn't match that model, the same exception of other invalid required values will be raised, and NVDA won't be restarted.

Testing strategy:

  • The addonId of the searchWith add-on has been removed from a cache file.
    • Pressed NVDA+q to restart NVDA.
  • Confirmed that NVDA was restarted befor aplying this fix, but not after.

Known issues with pull request:

None

Code Review Checklist:

  • Documentation:
    • Change log entry
    • User Documentation
    • Developer / Technical Documentation
    • Context sensitive help for GUI changes
  • Testing:
    • Unit tests
    • System (end to end) tests
    • Manual testing
  • UX of all users considered:
    • Speech
    • Braille
    • Low Vision
    • Different web browsers
    • Localization in other languages / culture than English
  • API is compatible with existing add-ons.
  • Security precautions taken.

return None
try:
data = cacheData["data"]
cachedAddonData = _createStoreCollectionFromJson(data)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see better exception handling in _createStoreCollectionFromJson. In particular, if we get a JSONEncodeError or KeyError due to malformed data, invalidate the cache.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that already occurring here for KeyError? Also I think you mean JSONDecodeError

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

except (KeyError, JSONDecodeError):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please change the exception handling below on line 189 to include JSONDecodeError

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seanbudd , I've included JSONDecodeError in the exception as you requested.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have you tested this? It doesnt seem like JSONDecodeError is imported from anywhere

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, sorry, I haven't tested it and in fact VS Code is raising a warning. I cannot test this now. I'll try to import JSONDecodeError

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seanbudd , I've imported JSONDecodeError and VS Code doesn't show warnings.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please let me know when you have tested the code - we do not review or merge untested code

@seanbudd seanbudd changed the title fixBadCachedAddons Recover safely if the add-on store cache is invalid Apr 29, 2024
@seanbudd seanbudd added the conceptApproved Similar 'triaged' for issues, PR accepted in theory, implementation needs review. label Apr 30, 2024
@nvdaes

nvdaes commented Apr 30, 2024

Copy link
Copy Markdown
Collaborator Author

I've used the suggestion provided by @gerald-hartig for handling exceptions. I think this is ready for review.
Note that I've added the changelog entry in NVDA 2024.2, since 2024.3 section was not present.

@nvdaes nvdaes marked this pull request as ready for review April 30, 2024 04:39
@nvdaes nvdaes requested a review from a team as a code owner April 30, 2024 04:39
@nvdaes nvdaes requested a review from michaelDCurran April 30, 2024 04:39
@AppVeyorBot

Copy link
Copy Markdown

See test results for failed build of commit 311ee62e77

Comment thread source/addonStore/dataManager.py Outdated
with open(cacheFilePath, 'r', encoding='utf-8') as cacheFile:
cacheData = json.load(cacheFile)
except Exception:
except (KeyError, JSONDecodeError):

@codeofdusk codeofdusk Apr 30, 2024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I think the bare except was okay here (back out this change). I was referring to guarding the json.loads call in _createStoreCollectionFromJson itself with a try/except, or ensure appropriate exception handling at all callsites.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's necessary, an error being raised outside of loading the cached data from file on startup will fail safely.

@nvdaes

nvdaes commented Apr 30, 2024

Copy link
Copy Markdown
Collaborator Author

@codeofdusk codeofdusk 2 minutes ago
No, I think the bare except was okay here (back out that change). I was referring to guarding the json.loads call in _createStoreCollectionFromJson itself with a try/except.

I'll revert my last commit. For any reason unit tests are failing now.

About the better handling of exception in _createStoreCollectionFromJson, I'm not sure if should be done here or in a different PR.

@AppVeyorBot

Copy link
Copy Markdown

See test results for failed build of commit 71c3f870d5

@nvdaes

nvdaes commented Apr 30, 2024

Copy link
Copy Markdown
Collaborator Author

System tests are failing, I don't now why.

@seanbudd seanbudd added this to the 2024.2 milestone May 1, 2024
@seanbudd

seanbudd commented May 1, 2024

Copy link
Copy Markdown
Member

@nvdaes - don't need to worry about unexpected failures

Automated checks will be run against your PR. If these fail, please review them. Sometimes system tests fail unexpectedly. If you believe the failure is unrelated, feel free to ignore it unless it is raised by a reviewer.

contributing docs

@nvdaes

nvdaes commented May 2, 2024

Copy link
Copy Markdown
Collaborator Author

@seanbudd, I've tested this now, trying to change the file encoding (without gettting actually errors), and removing an addonId.
I think thisis ready for review.

Comment thread source/addonStore/dataManager.py Outdated
if NVDAState.shouldWriteToDisk():
os.remove(cacheFilePath)
return None
from json import JSONDecodeError

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please import this from the base file, there is no need for this to be a lazy import

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@seanbudd seanbudd marked this pull request as draft May 6, 2024 05:39
@nvdaes nvdaes marked this pull request as ready for review May 7, 2024 03:12
@seanbudd seanbudd changed the base branch from master to beta May 7, 2024 05:44
seanbudd
seanbudd previously approved these changes May 7, 2024
@seanbudd

seanbudd commented May 8, 2024

Copy link
Copy Markdown
Member

@nvdaes - can you resolve merge conflicts here please?

@seanbudd

seanbudd commented May 8, 2024

Copy link
Copy Markdown
Member

Note that this is targeting beta - so please merge in origin/beta and move the change log entry to changes.md

@seanbudd

seanbudd commented May 8, 2024

Copy link
Copy Markdown
Member

It appears miscDeps is mistakenly updated in this PR

@nvdaes

nvdaes commented May 8, 2024

Copy link
Copy Markdown
Collaborator Author

@seanbudd , I have fixed conflicts and moved changelog entry to readme. Also, I run git submodule update and miscDeps are updated.

@nvdaes nvdaes force-pushed the fixBadCachedAddons branch from 2e311c4 to 8c50619 Compare May 8, 2024 04:22
@seanbudd

seanbudd commented May 8, 2024

Copy link
Copy Markdown
Member

@nvdaes - this Pr is now only the miscdeps changes

@seanbudd seanbudd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @nvdaes

@seanbudd seanbudd merged commit 28ce9b8 into nvaccess:beta May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

conceptApproved Similar 'triaged' for issues, PR accepted in theory, implementation needs review. queued for merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When the JSON cache file in the add-on store is incorrect, starting NVDA afterwards will always restart.

5 participants