Skip to content

fix(blockstore): add timeout and cancellation handling to Block Store calls - #4645

Closed
sahuadarsh0 wants to merge 1 commit into
RevenueCat:mainfrom
sahuadarsh0:fix/4643-blockstore-timeout
Closed

sahuadarsh0 wants to merge 1 commit into
RevenueCat:mainfrom
sahuadarsh0:fix/4643-blockstore-timeout

Conversation

@sahuadarsh0

@sahuadarsh0 sahuadarsh0 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Checklist

  • If applicable, unit tests
  • If applicable, create follow-up issues for purchases-ios and hybrids

Motivation

Resolves #4643

PurchasesOrchestrator waits on BlockstoreHelper.aliasCurrentAndStoredUserIdsIfNeeded before dispatching purchase callbacks and restore queries, and waits on BlockstoreHelper.clearUserIdBackupIfNeeded inside runIdentityOperation before calling identityManager.logIn. Previously, retrieveBytes, deleteBytes, and storeBytes waited indefinitely on Play Services Task callbacks with no timeout and no OnCanceledListener, so a stalled or canceled Task left purchase callbacks unsettled and could block subsequent identity operations.

Description

  • Added a private Task<T>.awaitWithTimeout() helper in BlockstoreHelper that wraps suspendCancellableCoroutine in withTimeout(blockstoreTimeout) (default 5.seconds) and handles addOnCanceledListener alongside success and failure listeners.
  • Updated getBlockstoreData, storeUserIdIfNeeded, and clearUserIdBackupIfNeeded to use awaitWithTimeout(), logging timeouts and falling through to the existing completion callback once (with late Task completions ignored by safeResume), while rethrowing non-timeout CancellationException.
  • Added unit tests in BlockstoreHelperTest covering timeout (including late completion after timeout) and Task cancellation for both aliasCurrentAndStoredUserIdsIfNeeded and clearUserIdBackupIfNeeded.

Testing

  • ./gradlew :purchases:testDefaultsDebugUnitTest --tests "com.revenuecat.purchases.blockstore.BlockstoreHelperTest" --tests "com.revenuecat.purchases.common.LogInReadTimeoutTest" detektAll (all 23 tests and detektAll passed)
  • ./gradlew :purchases:compileCustomEntitlementComputationDebugKotlin (passed)

Note

Medium Risk
Touches gating for purchase callbacks and pre-login Block Store cleanup in PurchasesOrchestrator; behavior change is bounded timeouts with graceful fallback rather than new identity rules.

Overview
Adds a 5-second timeout around all Google Play Block Store Task awaits via a new Task.awaitWithTimeout() helper, and wires cancellation through OnCanceledListener so stalled or canceled Play Services tasks no longer hang identity/purchase flows indefinitely.

retrieveBytes, storeBytes, and deleteBytes now go through that helper; timeout and cancel paths log and continue (e.g. still invoke completion callbacks once for alias/clear), while non-timeout CancellationException is rethrown. Unit tests mock cancel listeners and assert single callback on timeout, including late task completion after timeout.

Reviewed by Cursor Bugbot for commit ff9fb66. Bugbot is set up for automated code reviews on this repo. Configure here.

@sahuadarsh0
sahuadarsh0 requested a review from a team as a code owner November 26, 2026 12:22
@prilaga

prilaga commented Dec 2, 2026

Copy link
Copy Markdown

Hi @tonidero @vegaro @RCGitBot
This is a major bug, please fix it asap.

@tonidero

tonidero commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Hi @sahuadarsh0, sorry for the delay and thanks for the contribution! While we haven't been able to reproduce the original issue, we think this is safe so we've merged it in our internal repo and it's part of 06899c0. This will be part of our next release which will ship soon, so will close this.

@prilaga sorry to hear you're experiencing this. It would be great if you could provide more details on how often you're facing this, since it's been difficult to reproduce this and we would consider this an edge case, so we want to make sure you're experiencing this same issue. Feel free to contact us in our support page.

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.

Purchase callback and logIn wait on Block Store with no timeout

3 participants