Skip to content

[in_app_purchase_storekit] Fix data race in StoreKit 1 request completions - #13584

Open
hkarmoush wants to merge 1 commit into
flutter:mainfrom
hkarmoush:fix/193989-storekit-main-thread-race
Open

hkarmoush wants to merge 1 commit into
flutter:mainfrom
hkarmoush:fix/193989-storekit-main-thread-race

Conversation

@hkarmoush

@hkarmoush hkarmoush commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

StoreKit 1 delivers SKProductsRequest / refresh receipt responses on a background dispatch queue. The completions for startProductRequestProductIdentifiers and refreshReceiptReceiptProperties were running on that queue and mutating productsCache (an NSMutableDictionary) and requestHandlers (an NSHashTable). Neither is thread safe, and both are also used from the main thread (addPaymentPaymentMap, shouldAddStorePayment, and the Pigeon entry points), so overlapping requests could corrupt them and crash with EXC_BAD_ACCESS or an NSHashTable count underflow.

Both completions now run on the main thread. This also means the Pigeon replies are sent from the platform thread. If a completion is already on the main thread it runs inline, so behaviour for the existing synchronous test stubs is unchanged.

Added two tests that finish the request from DispatchQueue.global() and assert the plugin's completion arrives on the main thread. The StoreKit 1 suite passes (34 tests). The StoreKit 2 tests fail in my simulator environment with and without this change.

Fixes flutter/flutter#193989

Pre-Review Checklist

Footnotes

  1. Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses a data race in the in_app_purchase_storekit package by ensuring that StoreKit 1 product and receipt refresh request completions are executed on the main thread. A helper method runOnMainThread has been introduced to run blocks on the main thread, and the completion handlers for product requests and receipt refreshes have been updated to use this helper. Additionally, unit tests have been added to verify that these completions occur on the main thread, and the package version has been bumped to 0.4.17+1. There are no review comments to address.

@flutter-dashboard

Copy link
Copy Markdown

This pull request is not mergeable in its current state, likely because of a merge conflict. Pre-submit CI jobs were not triggered. Pushing a new commit to this branch that resolves the issue will result in pre-submit jobs being scheduled.

@LouiseHsu

Copy link
Copy Markdown
Contributor

can you fix the merge conflict, then I can run the cicd tests?

@hkarmoush
hkarmoush force-pushed the fix/193989-storekit-main-thread-race branch from edb39d7 to f5b1b3b Compare November 13, 2026 10:41
@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Nov 13, 2026
@LouiseHsu LouiseHsu added the CICD Run CI/CD label Nov 14, 2026
…tions

StoreKit calls product and receipt refresh request completions on a background queue, but they mutate productsCache and requestHandlers, which are also used from the main thread. Run the completions on the main thread.

Fixes flutter/flutter#193989
@hkarmoush
hkarmoush force-pushed the fix/193989-storekit-main-thread-race branch from f5b1b3b to 6814eac Compare November 15, 2026 16:57
@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Nov 15, 2026
@github-actions github-actions Bot added the P3 Issues that are less important to the Flutter project label Nov 15, 2026
@hkarmoush

Copy link
Copy Markdown
Contributor Author

Thanks @LouiseHsu, I rebased onto the latest main and fixed the conflict. It should be good to run the CI tests now.

@LouiseHsu LouiseHsu added the CICD Run CI/CD label Nov 16, 2026
@hkarmoush

Copy link
Copy Markdown
Contributor Author

Rebased onto main and resolved the conflict, so it should be ready for you to kick off CI. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD p: in_app_purchase P3 Issues that are less important to the Flutter project platform-ios platform-macos

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[in_app_purchase_storekit] Data race on productsCache / requestHandlers in StoreKit 1 product request completion (EXC_BAD_ACCESS)

2 participants