From 43f7781c87c3540f2ad09147ea3fb63765b72f01 Mon Sep 17 00:00:00 2001 From: Lulu Wu Date: Mon, 10 Jul 2023 13:37:34 -0700 Subject: [PATCH] Avoid duplicate destroy on same thread (#38233) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/38233 When call ReactHost.destroy multiple times on same thread, the synchronization we have now can not protect us from concurrent issues such as ```ConcurrentModificationException```, to avoid this case this diff checks if ReactInstance has been reset, if so it means an early destroy has been called and we should not destroy again. Changelog: [Android][Changed] - Avoid duplicate destroy on same thread Reviewed By: fkgozali Differential Revision: D47276191 fbshipit-source-id: 2291b89cb980ca762abddb835e703abd095a93b3 --- .../main/java/com/facebook/react/bridgeless/ReactHost.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactHost.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactHost.java index 84e4f82ab58..10ef6bc5234 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactHost.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridgeless/ReactHost.java @@ -1415,6 +1415,13 @@ public class ReactHost implements ReactHostInterface { raiseSoftException(method, reason, ex); synchronized (mReactInstanceTaskRef) { + // Prevent re-destroy when ReactInstance has been reset already, which could happen when + // calling destroy multiple times on the same thread + ReactInstance reactInstance = mReactInstanceTaskRef.get().getResult(); + if (reactInstance == null) { + return; + } + // Retain a reference to current ReactContext before de-referenced by mReactContextRef final ReactContext reactContext = getCurrentReactContext();