Commit a3e87c6
Fix Fabric out-of-order event delivery on Android
Summary:
Fixes #54636. Thanks to the Software Mansion team for the detailed reproduction and analysis.
Based on #56634
On Android Fabric, events emitted for a tag whose `EventEmitterWrapper` has not yet been set (e.g. the view is preallocated but not mounted) are buffered in a per-ViewState queue. When `updateEventEmitter` later sets the emitter, it drains the queue. However, events arriving between the queue-post and the drain could bypass the queue and dispatch directly, delivering events to JS out of receive order.
This change replaces the lambda-based `enqueuePendingEvent` (which deferred event buffering to a `runOnUiThread` lambda, leaving events "in limbo" in the Handler queue) with direct queue insertion under a `synchronized(viewState)` lock:
- `dispatchEvent` fast path: two volatile reads (`eventEmitter`, `pendingEventQueue`). If the emitter is set and no queue exists, dispatch directly — no lock, no allocation.
- `dispatchEvent` slow path (rare, during view init): `synchronized(viewState)` — re-check emitter under lock; if still null, create queue and enqueue the event. If the emitter became available between the volatile read and the lock, a barrier (`synchronized<Unit>(viewState) {}`) waits for any in-progress drain before dispatching.
- `updateEventEmitter`: `synchronized(viewState)` — set emitter, drain queue, null the queue reference. The queue stays non-null during the drain, so concurrent fast-path checks correctly fall through to the barrier.
Queue nullability (`pendingEventQueue == null`) is the cross-thread signal that replaces the previous `AtomicInteger` counter approach.
## Changelog:
[ANDROID][FIXED] - Fix Fabric out-of-order event delivery for events emitted before view mount
[ANDROID][BREAKING] - Removed SurfaceMountingManager#enqueuePendingEvent
Reviewed By: mdvacca
Differential Revision: D102822618
fbshipit-source-id: d683ef50e8c6bcd52cc947b3eda9cb2c1e1296ff
Co-authored-by: Mohanraj Venkatesan <mohanraj.venkatesan2304@gmail.com>1 parent 661ce06 commit a3e87c6
3 files changed
Lines changed: 391 additions & 84 deletions
File tree
- packages/react-native/ReactAndroid
- api
- src
- main/java/com/facebook/react/fabric/mounting
- test/java/com/facebook/react/fabric
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2281 | 2281 | | |
2282 | 2282 | | |
2283 | 2283 | | |
2284 | | - | |
2285 | | - | |
2286 | 2284 | | |
2287 | 2285 | | |
2288 | 2286 | | |
| |||
Lines changed: 38 additions & 82 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
13 | 12 | | |
14 | 13 | | |
15 | 14 | | |
| |||
965 | 964 | | |
966 | 965 | | |
967 | 966 | | |
| 967 | + | |
968 | 968 | | |
969 | | - | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
970 | 975 | | |
971 | 976 | | |
972 | 977 | | |
973 | 978 | | |
974 | 979 | | |
975 | | - | |
976 | | - | |
977 | | - | |
978 | | - | |
979 | | - | |
980 | | - | |
981 | | - | |
982 | | - | |
983 | | - | |
984 | 980 | | |
985 | 981 | | |
986 | 982 | | |
| |||
1190 | 1186 | | |
1191 | 1187 | | |
1192 | 1188 | | |
1193 | | - | |
1194 | | - | |
1195 | | - | |
1196 | | - | |
1197 | | - | |
1198 | | - | |
1199 | | - | |
1200 | | - | |
1201 | | - | |
1202 | | - | |
1203 | | - | |
1204 | | - | |
1205 | | - | |
1206 | | - | |
1207 | | - | |
1208 | | - | |
1209 | | - | |
1210 | | - | |
1211 | | - | |
1212 | | - | |
1213 | | - | |
1214 | | - | |
1215 | | - | |
1216 | | - | |
1217 | | - | |
1218 | | - | |
1219 | | - | |
1220 | | - | |
1221 | | - | |
1222 | | - | |
1223 | | - | |
1224 | | - | |
1225 | | - | |
1226 | | - | |
1227 | | - | |
1228 | | - | |
1229 | | - | |
1230 | | - | |
1231 | | - | |
1232 | | - | |
1233 | | - | |
1234 | | - | |
1235 | | - | |
1236 | | - | |
1237 | | - | |
1238 | | - | |
1239 | | - | |
1240 | | - | |
1241 | | - | |
1242 | | - | |
1243 | | - | |
1244 | 1189 | | |
1245 | 1190 | | |
1246 | 1191 | | |
| |||
1252 | 1197 | | |
1253 | 1198 | | |
1254 | 1199 | | |
1255 | | - | |
1256 | 1200 | | |
1257 | 1201 | | |
1258 | 1202 | | |
1259 | 1203 | | |
1260 | | - | |
1261 | | - | |
1262 | | - | |
1263 | | - | |
1264 | | - | |
1265 | | - | |
1266 | | - | |
1267 | | - | |
1268 | | - | |
1269 | | - | |
1270 | | - | |
1271 | | - | |
1272 | | - | |
1273 | | - | |
| 1204 | + | |
| 1205 | + | |
| 1206 | + | |
| 1207 | + | |
| 1208 | + | |
| 1209 | + | |
| 1210 | + | |
| 1211 | + | |
| 1212 | + | |
| 1213 | + | |
| 1214 | + | |
| 1215 | + | |
| 1216 | + | |
| 1217 | + | |
| 1218 | + | |
| 1219 | + | |
| 1220 | + | |
| 1221 | + | |
| 1222 | + | |
1274 | 1223 | | |
1275 | 1224 | | |
| 1225 | + | |
| 1226 | + | |
| 1227 | + | |
| 1228 | + | |
| 1229 | + | |
| 1230 | + | |
| 1231 | + | |
| 1232 | + | |
1276 | 1233 | | |
1277 | | - | |
| 1234 | + | |
1278 | 1235 | | |
1279 | | - | |
| 1236 | + | |
1280 | 1237 | | |
1281 | 1238 | | |
1282 | 1239 | | |
| |||
1304 | 1261 | | |
1305 | 1262 | | |
1306 | 1263 | | |
1307 | | - | |
1308 | | - | |
1309 | | - | |
| 1264 | + | |
| 1265 | + | |
1310 | 1266 | | |
1311 | 1267 | | |
1312 | 1268 | | |
| |||
0 commit comments