Repository navigation
fix(ios): warm up RCTImageLoader before parallel image loads - #4373
Conversation
mfazekas
left a comment
There was a problem hiding this comment.
Thanks for digging into this and for the reproducer.
It's unfortunate that we have to work around this at all, since these are bugs in React Native's RCTImageLoader, whose protocol says it can be called from any thread. There are two first-call races there:
_loaders/_decoders, which is what the #3725 crashes inRCTImageFromLocalAssetURLcome from. Fixed in react/react-native#57297, shipped in RN 0.87.- the lazy
imageCachethis PR targets. Fixed in react/react-native#57971, not released yet.
Both only bite on the first concurrent use of a fresh loader, so rather than serializing every load, could we warm the loader up once instead? For example, on first use in RNMBXImageQueue, do a single loadImage of a 1×1 data: PNG (with a partialLoadBlock, so the cache path runs too) before starting the parallel loads. That initializes _loaders, _decoders and imageCache through public API, keeps loads parallel, and is easy to delete once our minimum RN includes both fixes. A one-line comment pointing at the two RN PRs is enough.
Could you also mention #57297 in the description, and if possible check the warm-up against your swizzle reproducer?
|
Thanks, that is a better approach. Switched to a one-time warm-up per |
mfazekas
left a comment
There was a problem hiding this comment.
Thanks, this is what I had in mind. The warm-up is small, runs once per loader, and loads are parallel again. I checked it against RCTImageLoader: the synchronous part of the data: PNG load sets up _loaders and, with the partialLoadBlock, imageCache. _decoders is then set up on the serial request queue, where our decodes happen anyway. The completion isn't awaited and the lock is only taken off the main thread, so I don't see a deadlock or hang risk.
Two small things for a follow-up. Could the comment say it can be removed once our minimum RN includes both #57297 and #57971, so whoever cleans it up later knows when? And warmUp could be fileprivate, since only the operation in this file calls it.
Again, it's unfortunate we need this at all for what is an RN bug, but this keeps the workaround cheap and easy to delete.
|
Thanks for the review and the merge. Both follow-ups are in #4377. |
Description
Related to #3725.
RNMBXImageQueuestarts its loads onDispatchQueue.global, so an<Images>with many entries calls a newRCTImageLoaderfrom many threads at once. Two lazy initializations inRCTImageLoaderare not safe for that first concurrent use:_loaders/_decoders: fix(image): fix data race in imageURLLoaderForURL: and imageDataDecoderForData: react/react-native#57297, shipped in RN 0.87imageCache: fix(iOS): reuse prefetched images react/react-native#57971, not released yetThe
imageCacheone crashes like this:This PR warms each new
RCTImageLoaderup once before the parallel loads: the first load is a 1×1data:PNG with apartialLoadBlock, made under a lock. Its synchronous part sets up_loadersandimageCache._decodersis set up when that image is decoded, on the loader's serial request queue, which is also where network anddata:images get decoded. After that the loads run in parallel as before. The warm-up is keyed per loader because a JS reload creates a new one.Tested with the reproducer below in
/example(RN 0.84.1, iPhone 17 Pro simulator, iOS 26.5). On the simulator the race does not fire on its own, so I widened its window with a temporary swizzle of-[RCTImageLoader imageCache](not part of this PR):mainChecklist
CONTRIBUTING.mdyarn generatein the root folder (no changes)/exampleapp./example)iOS only.
Component to reproduce the issue you're fixing
Register it as the root component in
example/index.js(it reloads JS after every run so each run gets a newRCTImageLoader), then press Start.Temporary swizzle that widens the race window (AppDelegate.mm)
Call it at the top of
application:didFinishLaunchingWithOptions:and launch withSIMCTL_CHILD_REPRO_WIDEN_RACE=1 xcrun simctl launch <udid> org.reactjs.native.example.RNMapboxExample.