Session Import Race Condition#308
Open
annrose2277-glitch wants to merge 1 commit into
Open
Conversation
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.
The initial session restoration logic handled critical storage decryption and network operations (supabase.auth) inside withContext(Dispatchers.Main). On slower TV chipsets, this choked the main thread and locked the UI loop.
Fragile Timeouts: Because the main thread was heavily congested, the hardcoded sequential withTimeoutOrNull barriers (2.5s, 2.5s, 3s) routinely expired before the database/storage layer could complete the handshake, treating a valid session as an authentication failure.
closes #300
🛠️ Solution
Refactored the checkAuthState() initialization flow to guarantee thread safety and robust recovery:
Offloaded to Background Thread: Shifted all Supabase storage and network invocations from Dispatchers.Main to Dispatchers.IO to ensure unhindered execution and zero UI blocking.
Structured Retry Architecture: Replaced the fragile, hardcoded sequential timeouts with an explicit retry loop (3 attempts) coupled with a minor backoff delay. This allows the system to recover gracefully if disk or network I/O stutters during early boot.
Maintained Recovery Fallback Sequence: Safely preserved the original cascade logic: attempting local storage recovery first, cascading to explicit token importation, and finally ensuring session validity.
📁 Files Modified
[INSERT_FILE_PATH_HERE] (e.g., app/src/main/kotlin/com/arflix/tv/data/repository/AuthRepository.kt)
✅ Impact & Benefits
Deterministic Authentication: Eradicates the race condition entirely, guaranteeing that valid persistent user sessions are consistently recognized on startup.
Improved Cold-Start Stability: Eliminates random logouts on slow or cold-booted devices, drastically upgrading the initial user experience.
Fluid UI Initialization: Relieving the main thread during boot prevents app-not-responding (ANR) flags and stuttering splash animations.