Repository navigation
Fix race in UiThreadUtil main handler lazy initialization - #58963
locnp-active wants to merge 1 commit into
Conversation
|
Hi @locnp-active! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
Summary:
UiThreadUtilkeeps its main-threadHandlerin a property delegated tolazy(LazyThreadSafetyMode.NONE):NONEcreates anUnsafeLazyImpl, whose getter is:mainHandleris first read by whichever thread first callsrunOnUiThread(),getUiThreadHandler()orremoveOnUiThread().isOnUiThread()and theassert*helpers only useLooper, so the main thread calling them does not initialize it. In practice the first reads usually come from background threads posting to the UI thread, such as the JS thread, the native modules thread, or a library's executor. When two of them make their first call at the same time:_value === UNINITIALIZED_VALUEand starts the initializer.UNINITIALIZED_VALUE, because A has not written_valueyet._valueand setsinitializer = null.initializer!!()and throwsNullPointerException.The same NPE can happen without the two calls overlapping in time.
_valueandinitializerare plain fields, so on a weakly ordered CPU (ARM) another thread can observeinitializer == nullbefore it observes the new_value.The exception is thrown from the accessor Kotlin generates for the delegated property, so crash reports show the top frame as
com.facebook.react.bridge.UiThreadUtil.getMainHandler. Only the first access in a process can race, so the crash is rare but fatal and usually happens during startup.This was introduced in #50536 (1033584), which migrated
UiThreadUtilto Kotlin. The Java implementation used double-checked locking on avolatilefield. A reviewer suggested plainby lazy { }, which is synchronized by default, but the merged code usesLazyThreadSafetyMode.NONE. Every release from 0.80 to 0.88.0-rc.4 includes it.This PR switches the property to
LazyThreadSafetyMode.SYNCHRONIZED, restoring the guarantee of the Java implementation. The lock is only taken until the value is initialized. After that, reads are a single volatile read, so therunOnUiThreadhot path is effectively unchanged.Changelog:
[ANDROID] [FIXED] - Fix crash in
UiThreadUtilwhen the main handler is first accessed from multiple threads at onceTest Plan:
The race only happens on the first access in a process and cannot be reproduced reliably in a unit test. It can be confirmed by:
UnsafeLazyImpl(NONE) is documented as unsafe for multi-threaded access.SynchronizedLazyImpl(SYNCHRONIZED) guarantees a single initialization and safe publication.UiThreadUtil.<clinit>creates the lazy withLazyThreadSafetyMode.NONE. With this change it usesSYNCHRONIZED.