Skip to content

Session Import Race Condition#308

Open
annrose2277-glitch wants to merge 1 commit into
ProdigyV21:mainfrom
annrose2277-glitch:feature
Open

Session Import Race Condition#308
annrose2277-glitch wants to merge 1 commit into
ProdigyV21:mainfrom
annrose2277-glitch:feature

Conversation

@annrose2277-glitch
Copy link
Copy Markdown

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.

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.

Session Import Race Condition

1 participant