[PM-29309] [BWA-209] Fix TOTP countdown freeze when returning to Authenticator app (change Flow to StateFlow) #6246
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.
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.