fix: Exception in exception handler, handling of 401 in reviewer #1098
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 an error users were occasionally getting in the reviewer when not logged in.
https://ankihub.sentry.io/issues/6253435393/events/f027550d1cb344b9ad9a5cb6b6c3aa9c/?project=6546414
Also makes some other improvements to the handling of 401 exceptions in the reviewer.
Related issues
https://ankihub.sentry.io/issues/6253435393/events/f027550d1cb344b9ad9a5cb6b6c3aa9c/?project=6546414
https://community.ankihub.net/t/got-an-error-message/397521
Proposed changes
The error was caused by our exception handler raising an exception while handling an 401
AnkiHubHTTPError.The exception handler raised an exception because it was calling
config.save_token(), which in turn called the callbacks inconfig.token_change_hook.One of the callbacks (
update_feature_flags_in_background) calledaqt.mw.taskman.run_in_background.aqt.mw.taskman.run_in_backgroundruns any pending closures from tasks that just finished.One of the pending closures re-raised an exception which was raised in the associated background task.
Summarizing, the issue is that calling
aqt.mw.taskman.run_in_background()can raise an exception if there were was an exception in a (possibly unrelated) background task that was just run. (When this happens, the new background task is not run.)This is something that should probably be fixed in Anki.
For now, the fix that this PR applies is to use
aqt.mw.taskman.run_on_main()to call functions which callaqt.mw.taskman.run_in_background()in some relevant places. Usingaqt.mw.taskman.run_on_main(closure)prevents exceptions raised by the closure from getting backpropagated to the caller.So, the code which calls
aqt.mw.taskman.run_in_background, won't be interrupted the exception. (The background task which it tried to start won't be run though.)We used this technique before in this decorator:
ankihub_addon/ankihub/gui/operations/utils.py
Line 21 in bd9f882
token_change_hookcallbacks when callingconfig.save_token()Other fixes/improvements:
Notes
Logs which were helpful while debugging the error:
https://app.datadoghq.com/logs?query=host%3Ashannons-air.dhcp.swmed.org%20service%3Aankihub_addon&agg_m=count&agg_m_source=base&agg_t=count&cols=host%2Cservice&context_event=AZUBBKq2AABE-P7_kAS1CQAK&fromUser=true&messageDisplay=inline&refresh_mode=sliding&storage=hot&stream_sort=time%2Casc&viz=&from_ts=1739478517555&to_ts=1739480376381&live=false