Skip to content

Commit 708f527

Browse files
javachemeta-codesync[bot]
authored andcommitted
Ensure JVM thread is attached when destroying FabricMountingManager (facebook#54218)
Summary: Pull Request resolved: facebook#54218 Observed some crashes coming from `schedulerDidRequestPreliminaryViewAllocation` which seemed to point at the FabricMountingManager being destroyed from the Hades GC thread. That thread is not attached to the JVM, so would crash when trying to destroy this global_ref. Changelog: [Internal] Reviewed By: lenaic Differential Revision: D85143603 fbshipit-source-id: 2ecd42d57188e6f3d69a6124e21ee8913b3d5b89
1 parent f4a9422 commit 708f527

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricMountingManager.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ FabricMountingManager::FabricMountingManager(
3535
jni::global_ref<JFabricUIManager::javaobject>& javaUIManager)
3636
: javaUIManager_(javaUIManager) {}
3737

38+
FabricMountingManager::~FabricMountingManager() {
39+
// Manually reset `javaUIManager_` since FabricMountingManager may be retained
40+
// from a GC thread (destroyUnmountedShadowNode)
41+
jni::ThreadScope::WithClassLoader([&]() { javaUIManager_.reset(); });
42+
}
43+
3844
void FabricMountingManager::onSurfaceStart(SurfaceId surfaceId) {
3945
std::lock_guard lock(allocatedViewsMutex_);
4046
allocatedViewRegistry_.emplace(
@@ -905,7 +911,7 @@ void FabricMountingManager::destroyUnmountedShadowNode(
905911

906912
// ThreadScope::WithClassLoader is necessary because
907913
// destroyUnmountedShadowNode is being called from a destructor thread
908-
facebook::jni::ThreadScope::WithClassLoader([&]() {
914+
jni::ThreadScope::WithClassLoader([&]() {
909915
static auto destroyUnmountedView =
910916
JFabricUIManager::javaClassStatic()->getMethod<void(jint, jint)>(
911917
"destroyUnmountedView");

packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricMountingManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class FabricMountingManager final {
2525
FabricMountingManager(
2626
jni::global_ref<JFabricUIManager::javaobject>& javaUIManager);
2727
FabricMountingManager(const FabricMountingManager&) = delete;
28+
~FabricMountingManager();
2829

2930
void onSurfaceStart(SurfaceId surfaceId);
3031

0 commit comments

Comments
 (0)