This repository was archived by the owner on Apr 26, 2026. It is now read-only.
Commit f14d652
fix(android,ios): add thread safety to listener operations (#3157)
## Summary
- iOS: Apply `NSLock` + snapshot pattern to all listener arrays for
thread safety
- Android: Apply snapshot pattern to `userChoiceBilling` /
`developerProvidedBilling` listeners for consistency
- Add thread safety unit tests and multi-listener registration/removal
tests
## Changes
### iOS (`ios/HybridRnIap.swift`)
- Add `listenerLock` (`NSLock`) for synchronized access to listener
arrays
- Wrap all add/remove listener methods (`addPurchaseUpdatedListener`,
`addPurchaseErrorListener`, `addPromotedProductListenerIOS`, and their
remove counterparts) with `listenerLock.withLock`
- `sendPurchaseUpdate`, `sendPurchaseError`: iterate over snapshot copy
instead of live array (matches Android pattern)
- `sendPurchaseError`: protect error dedup state
(`lastPurchaseErrorKey`, `lastPurchaseErrorTimestamp`) inside lock since
they are accessed from multiple threads
- Apply snapshot pattern to promoted product listener iteration
- `cleanupExistingState`: clear listener arrays and reset error dedup
state inside lock
### Android (`android/.../HybridRnIap.kt`)
- `sendUserChoiceBilling`, `sendDeveloperProvidedBilling`: change from
direct iteration to snapshot pattern
- `endConnection`: add `synchronized` for clearing
`userChoiceBillingListenersAndroid` and
`developerProvidedBillingListenersAndroid`
### Tests
- **Android**: `ListenerThreadSafetyTest.kt` (new) - 5 tests verifying
concurrent add/remove/iterate safety
- **TypeScript**: `index.test.ts` - 4 new tests verifying multi-listener
registration/removal independence
## Test plan
- [x] `yarn typecheck` passes
- [x] `yarn lint` passes
- [x] `yarn test` passes (251 tests, 12 suites)
- [x] Pre-commit hooks pass
Closes #3150
🤖 Generated with [Claude Code](https://claude.ai/code)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
## Release Notes
* **Bug Fixes**
* Enhanced thread-safety mechanisms for in-app purchase event listeners
on iOS and Android to prevent potential crashes when listeners are
added, removed, or notified concurrently.
* **Tests**
* Added comprehensive test suite validating thread-safe listener
operations, snapshot semantics, and reliable event delivery across
multiple concurrent listeners.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>1 parent cc4de64 commit f14d652
4 files changed
Lines changed: 317 additions & 58 deletions
File tree
- android/src
- main/java/com/margelo/nitro/iap
- test/java/com/margelo/nitro/iap
- ios
- src/__tests__
Lines changed: 6 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
258 | 258 | | |
259 | 259 | | |
260 | 260 | | |
261 | | - | |
262 | | - | |
| 261 | + | |
| 262 | + | |
263 | 263 | | |
264 | 264 | | |
265 | 265 | | |
| |||
1592 | 1592 | | |
1593 | 1593 | | |
1594 | 1594 | | |
1595 | | - | |
1596 | | - | |
1597 | | - | |
| 1595 | + | |
| 1596 | + | |
1598 | 1597 | | |
1599 | 1598 | | |
1600 | 1599 | | |
| |||
1611 | 1610 | | |
1612 | 1611 | | |
1613 | 1612 | | |
1614 | | - | |
1615 | | - | |
1616 | | - | |
| 1613 | + | |
| 1614 | + | |
1617 | 1615 | | |
1618 | 1616 | | |
1619 | 1617 | | |
| |||
Lines changed: 154 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
27 | 29 | | |
28 | 30 | | |
29 | 31 | | |
| |||
851 | 853 | | |
852 | 854 | | |
853 | 855 | | |
854 | | - | |
855 | | - | |
| 856 | + | |
| 857 | + | |
856 | 858 | | |
857 | 859 | | |
858 | 860 | | |
| |||
864 | 866 | | |
865 | 867 | | |
866 | 868 | | |
867 | | - | |
| 869 | + | |
868 | 870 | | |
869 | | - | |
870 | | - | |
871 | | - | |
| 871 | + | |
872 | 872 | | |
873 | | - | |
| 873 | + | |
874 | 874 | | |
875 | | - | |
| 875 | + | |
876 | 876 | | |
877 | | - | |
| 877 | + | |
878 | 878 | | |
879 | | - | |
| 879 | + | |
880 | 880 | | |
881 | | - | |
| 881 | + | |
882 | 882 | | |
883 | | - | |
| 883 | + | |
884 | 884 | | |
885 | | - | |
886 | | - | |
887 | | - | |
| 885 | + | |
888 | 886 | | |
889 | | - | |
| 887 | + | |
890 | 888 | | |
891 | | - | |
892 | | - | |
893 | | - | |
| 889 | + | |
894 | 890 | | |
895 | 891 | | |
896 | 892 | | |
| |||
954 | 950 | | |
955 | 951 | | |
956 | 952 | | |
| 953 | + | |
957 | 954 | | |
958 | | - | |
| 955 | + | |
959 | 956 | | |
960 | 957 | | |
961 | 958 | | |
962 | 959 | | |
963 | 960 | | |
| 961 | + | |
964 | 962 | | |
965 | 963 | | |
966 | 964 | | |
967 | 965 | | |
968 | 966 | | |
969 | 967 | | |
970 | | - | |
| 968 | + | |
971 | 969 | | |
972 | 970 | | |
973 | 971 | | |
| |||
992 | 990 | | |
993 | 991 | | |
994 | 992 | | |
995 | | - | |
996 | | - | |
997 | | - | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
998 | 1008 | | |
999 | 1009 | | |
1000 | | - | |
1001 | | - | |
1002 | | - | |
1003 | | - | |
1004 | | - | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
1005 | 1013 | | |
1006 | 1014 | | |
1007 | | - | |
| 1015 | + | |
1008 | 1016 | | |
1009 | 1017 | | |
1010 | 1018 | | |
1011 | | - | |
| 1019 | + | |
1012 | 1020 | | |
1013 | | - | |
1014 | 1021 | | |
1015 | 1022 | | |
1016 | 1023 | | |
1017 | 1024 | | |
1018 | | - | |
1019 | | - | |
1020 | | - | |
1021 | | - | |
1022 | | - | |
1023 | 1025 | | |
1024 | | - | |
1025 | | - | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
1026 | 1038 | | |
1027 | 1039 | | |
1028 | 1040 | | |
1029 | 1041 | | |
1030 | 1042 | | |
1031 | 1043 | | |
1032 | | - | |
| 1044 | + | |
| 1045 | + | |
1033 | 1046 | | |
1034 | 1047 | | |
1035 | 1048 | | |
| |||
1067 | 1080 | | |
1068 | 1081 | | |
1069 | 1082 | | |
1070 | | - | |
1071 | | - | |
1072 | | - | |
1073 | | - | |
1074 | | - | |
1075 | | - | |
1076 | | - | |
1077 | | - | |
1078 | | - | |
| 1083 | + | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
| 1088 | + | |
| 1089 | + | |
| 1090 | + | |
| 1091 | + | |
| 1092 | + | |
| 1093 | + | |
1079 | 1094 | | |
1080 | 1095 | | |
1081 | 1096 | | |
| |||
0 commit comments