Do not synchronize on java.lang.Boolean. (#56196)

Summary:
## Summary:  Do not synchronize on java.lang.Boolean.

Once JEP 401 is implemented synchronization on
value classes (which box classes are) will throw IdentityException.
In general synchronization on 1) mutable field 2) box class is
inefficient (instances are shared) at best and a bug at worst case
as synchronization is done holding different monitors (it might be
fine, but is hard to reason about).

## Changelog:

[ANDROID] [FIXED] Use explicit `ReactInstanceManager.mHasStartedDestroyingLock` instead of using `ReactInstanceManager.mHasStartedDestroying`

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests

Pull Request resolved: https://github.com/react/react-native/pull/56196

Test Plan: GHA

Reviewed By: cortinico

Differential Revision: D110168816

Pulled By: javache

fbshipit-source-id: dae081dc00a94859f19dcc3607971a9c3d6fbd44
This commit is contained in:
Almaz authored and React Native Bot committed 2026-07-23 20:13:11 +00:00
1 parent 36f69eff0d
commit 821045a24f
1 file changed
+5 -4
@@ -171,6 +171,7 @@ public class ReactInstanceManager {
// Identifies whether the instance manager destroy function is in process,
// while true any spawned create thread should wait for proper clean up before initializing
private volatile Boolean mHasStartedDestroying = false;
private final Object mHasStartedDestroyingLock = new Object();
private final MemoryPressureRouter mMemoryPressureRouter;
private final @Nullable JSExceptionHandler mJSExceptionHandler;
private final @Nullable UIManagerProvider mUIManagerProvider;
@@ -782,8 +783,8 @@ public class ReactInstanceManager {
ResourceDrawableIdHelper.getInstance().clear();
mHasStartedDestroying = false;
synchronized (mHasStartedDestroying) {
mHasStartedDestroying.notifyAll();
synchronized (mHasStartedDestroyingLock) {
mHasStartedDestroyingLock.notifyAll();
}
synchronized (mPackages) {
mViewManagerNames = null;
@@ -1139,10 +1140,10 @@ public class ReactInstanceManager {
null,
() -> {
ReactMarker.logMarker(REACT_CONTEXT_THREAD_END);
synchronized (ReactInstanceManager.this.mHasStartedDestroying) {
synchronized (ReactInstanceManager.this.mHasStartedDestroyingLock) {
while (ReactInstanceManager.this.mHasStartedDestroying) {
try {
ReactInstanceManager.this.mHasStartedDestroying.wait();
ReactInstanceManager.this.mHasStartedDestroyingLock.wait();
} catch (InterruptedException e) {
// Interrupted while waiting for destruction to complete, just retry
continue;