fix(deck-picker): do not block the main thread on startup during sync#21334
Open
criticalAY wants to merge 1 commit into
Open
fix(deck-picker): do not block the main thread on startup during sync#21334criticalAY wants to merge 1 commit into
criticalAY wants to merge 1 commit into
Conversation
DeckPicker recreation (for example a light/dark theme change) ran InitialActivity.getStartupFailureType on the main thread, which waits on the collection queue. A sync stuck on an unresponsive server holds that queue for the entire network call, so the recreated activity froze and ANRed after 5 seconds. Run the check on Dispatchers.IO and deliver the result through flowOfStartupResponse instead. Assisted-by: Claude Opus 4.8 (some part of the PR)
Member
|
Could you provide reproduction instructions (probably a The startup path has historically been nasty. This initially looks good (and the ViewModel makes things easier), but I'd like to run it through a debugger to confirm. |
Contributor
Author
Subject: [PATCH] repro: sync anr
---
Index: AnkiDroid/src/main/java/com/ichi2/anki/Sync.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/Sync.kt b/AnkiDroid/src/main/java/com/ichi2/anki/Sync.kt
--- a/AnkiDroid/src/main/java/com/ichi2/anki/Sync.kt (revision 25da95364f62624be98404df64afae4d699f134c)
+++ b/AnkiDroid/src/main/java/com/ichi2/anki/Sync.kt (date 1783413600156)
@@ -159,6 +159,8 @@
manualCancelButton = R.string.dialog_cancel,
) {
withCol {
+ Timber.w("#21305 repro: holding the collection queue for 60s")
+ Thread.sleep(60_000)
syncCollection(auth2, syncMedia = false) // media is synced by SyncMediaWorker
}
}
Index: AnkiDroid/src/main/java/com/ichi2/anki/deckpicker/DeckPickerViewModel.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/deckpicker/DeckPickerViewModel.kt b/AnkiDroid/src/main/java/com/ichi2/anki/deckpicker/DeckPickerViewModel.kt
--- a/AnkiDroid/src/main/java/com/ichi2/anki/deckpicker/DeckPickerViewModel.kt (revision 25da95364f62624be98404df64afae4d699f134c)
+++ b/AnkiDroid/src/main/java/com/ichi2/anki/deckpicker/DeckPickerViewModel.kt (date 1783413571943)
@@ -522,17 +522,11 @@
}
Timber.d("handleStartup: Continuing after permission granted")
- viewModelScope.launch {
- // opening the collection waits on the collection queue, which a sync stuck on an
- // unresponsive server can hold for a long time. Waiting on the main thread here
- // froze the DeckPicker when it was recreated
- val failure =
- withContext(Dispatchers.IO) {
- InitialActivity.getStartupFailureType(environment::initializeAnkiDroidFolder)
- }
+ run {
+ val failure = InitialActivity.getStartupFailureType(environment::initializeAnkiDroidFolder)
if (failure != null) {
flowOfStartupResponse.value = StartupResponse.FatalError(failure)
- return@launch
+ return
}
// successful startup
|
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.
Note
Assisted-by: Claude Opus 4.8 (some diagnosis and a few parts of code)
Purpose / Description
Fixes an ANR when the DeckPicker is recreated while a sync is stuck on an unresponsive server (for example AnkiWeb being down).
Fixes
Approach
See commit
How Has This Been Tested?
Unit test
Learning (optional, can help others)
NA
Checklist
Please, go through these checks before submitting the PR.