[PM-29309] [BWA-209] bug: Fix TOTP countdown freeze when returning to Authenticator app (change Flow to StateFlow)#6764
Merged
david-livefront merged 1 commit intomainfrom Apr 6, 2026
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #6764 +/- ##
==========================================
- Coverage 85.57% 85.31% -0.27%
==========================================
Files 1045 949 -96
Lines 61504 60588 -916
Branches 8638 8573 -65
==========================================
- Hits 52632 51690 -942
- Misses 5869 5923 +54
+ Partials 3003 2975 -28
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Contributor
|
New Issues (128)Checkmarx found the following issues in this Pull Request
|
SaintPatrck
reviewed
Apr 6, 2026
SaintPatrck
reviewed
Apr 6, 2026
| * @return A [AuthenticatorItem] with mock data based on the provided number. | ||
| */ | ||
| @Suppress("LongParameterList") | ||
| fun createMockAuthenticatorItem( |
Refactor TotpCodeManager to use per-item StateFlow caching, matching the Password Manager's pattern. This prevents flow recreation on resubscribe and removes the 5-second stop timeout that caused stale state when returning from background. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
3e47f31 to
06c4920
Compare
SaintPatrck
approved these changes
Apr 6, 2026
Collaborator
Author
|
Thanks for the review @SaintPatrck |
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.


From the original PR found here: #6246
Disclaimer: This PR was created with AI assisted agent (Claude Code) but manually checked myself to my best knowledge and this repository standards. If you feel this is against contribution guidelines (I haven't found anything related to such) feel free to disregard this PR. I honestly believe this is valid PR, and I tested it manually as well.
This fix OTP code freeze when returning to Authenticator app from background - issue #6244
when occasionally user can experience frozen TOTP codes and countdown timers when returning to the Authenticator app from background. The codes remain static and don't update until the screen is manually refreshed. This changes flow implementation in AUthenticator app (Flow) to one that is Password Manager (StateFlow).
Root Cause
The bug stems from a flow lifecycle mismatch in
TotpCodeManagerImpl. The TOTP code update mechanism relies on continuous Flow emissions with a 1-second delay loop, but this flow stops when the app goes to background.The race condition:
collectAsStateWithLifecycle()stops collectingSharingStarted.WhileSubscribed(5_000L)stops upstream flowsdelay(ONE_SECOND_MILLISECOND)timer loop stops entirelystateInvalue briefly displayed before new emissionscombine()waits for all flows to emitSolution
Aligned
TotpCodeManagerImplwith the Password Manager's proven pattern:mutableMapOf<AuthenticatorItem, StateFlow<...>>— Prevents flow recreation on resubscribeSharingStarted.WhileSubscribed()without delaySharingStarted.Eagerlyfor per-item flows — Ensures cached per-item flows continue emitting even when the combined flow changes (e.g., when adding new items)Changes
TotpCodeManager.kt— Changed return type fromFlow<List<VerificationCodeItem>>toStateFlow<List<VerificationCodeItem>>TotpCodeManagerImpl.kt— AddedDispatcherManagerdependency, per-item StateFlow caching withgetOrCreateItemStateFlow(), and cleanup handlersAuthenticatorManagerModule.kt— PassesdispatcherManagertoTotpCodeManagerImplconstructorAuthenticatorRepositoryImpl.kt— Removed 5-second timeout, now usesSharingStarted.WhileSubscribed()without delayMutableStateFlow()instead offlowOf()Testing
I wasn't able to recreate issue after this fix.