Ensure JVM thread is attached when destroying FabricMountingManager (#54218)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/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
This commit is contained in:
Pieter De Baets
2025-10-21 06:33:11 -07:00
committed by meta-codesync[bot]
parent f4a942254a
commit 708f527ec7
2 changed files with 8 additions and 1 deletions
@@ -35,6 +35,12 @@ FabricMountingManager::FabricMountingManager(
jni::global_ref<JFabricUIManager::javaobject>& javaUIManager)
: javaUIManager_(javaUIManager) {}
FabricMountingManager::~FabricMountingManager() {
// Manually reset `javaUIManager_` since FabricMountingManager may be retained
// from a GC thread (destroyUnmountedShadowNode)
jni::ThreadScope::WithClassLoader([&]() { javaUIManager_.reset(); });
}
void FabricMountingManager::onSurfaceStart(SurfaceId surfaceId) {
std::lock_guard lock(allocatedViewsMutex_);
allocatedViewRegistry_.emplace(
@@ -905,7 +911,7 @@ void FabricMountingManager::destroyUnmountedShadowNode(
// ThreadScope::WithClassLoader is necessary because
// destroyUnmountedShadowNode is being called from a destructor thread
facebook::jni::ThreadScope::WithClassLoader([&]() {
jni::ThreadScope::WithClassLoader([&]() {
static auto destroyUnmountedView =
JFabricUIManager::javaClassStatic()->getMethod<void(jint, jint)>(
"destroyUnmountedView");
@@ -25,6 +25,7 @@ class FabricMountingManager final {
FabricMountingManager(
jni::global_ref<JFabricUIManager::javaobject>& javaUIManager);
FabricMountingManager(const FabricMountingManager&) = delete;
~FabricMountingManager();
void onSurfaceStart(SurfaceId surfaceId);