Skip to content

Commit e51a5f0

Browse files
Nick Lefeverfacebook-github-bot
authored andcommitted
Enable RSNRU only for React commits (#51843)
Summary: Pull Request resolved: #51843 Changelog: [Internal] To avoid corrupting the React fiber tree when committing from multiple threads this diff only updates shadow node references within the fiber tree for commits originating from React. This guarantees that during the update of the references no React render will start or is running, making the update of the shadow node reference safe to execute. S527994 Reviewed By: sammy-SC Differential Revision: D76043845 fbshipit-source-id: bfcbeaae7fc8b976a1c2db6682330cef9ca25ab8
1 parent fe66694 commit e51a5f0

4 files changed

Lines changed: 14 additions & 7 deletions

File tree

packages/react-native/Libraries/ReactNative/__tests__/State-ForcedCloneCommitHook-itest.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*
77
* @fantom_flags useShadowNodeStateOnClone:true
8+
* @fantom_flags updateRuntimeShadowNodeReferencesOnCommit:*
89
* @flow strict-local
910
* @format
1011
*/

packages/react-native/ReactCommon/react/renderer/mounting/ShadowTree.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ CommitStatus ShadowTree::tryCommit(
318318
std::scoped_lock dispatchLock(EventEmitter::DispatchMutex());
319319
updateMountedFlag(
320320
currentRevision_.rootShadowNode->getChildren(),
321-
newRootShadowNode->getChildren());
321+
newRootShadowNode->getChildren(),
322+
commitOptions.source);
322323
}
323324

324325
telemetry.didCommit();

packages/react-native/ReactCommon/react/renderer/mounting/updateMountedFlag.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
namespace facebook::react {
1313
void updateMountedFlag(
1414
const ShadowNode::ListOfShared& oldChildren,
15-
const ShadowNode::ListOfShared& newChildren) {
15+
const ShadowNode::ListOfShared& newChildren,
16+
ShadowTreeCommitSource commitSource) {
1617
// This is a simplified version of Diffing algorithm that only updates
1718
// `mounted` flag on `ShadowNode`s. The algorithm sets "mounted" flag before
1819
// "unmounted" to allow `ShadowNode` detect a situation where the node was
@@ -49,11 +50,13 @@ void updateMountedFlag(
4950
newChild->setMounted(true);
5051
oldChild->setMounted(false);
5152

52-
if (ReactNativeFeatureFlags::updateRuntimeShadowNodeReferencesOnCommit()) {
53+
if (commitSource == ShadowTreeCommitSource::React &&
54+
ReactNativeFeatureFlags::updateRuntimeShadowNodeReferencesOnCommit()) {
5355
newChild->updateRuntimeShadowNodeReference(newChild);
5456
}
5557

56-
updateMountedFlag(oldChild->getChildren(), newChild->getChildren());
58+
updateMountedFlag(
59+
oldChild->getChildren(), newChild->getChildren(), commitSource);
5760
}
5861

5962
size_t lastIndexAfterFirstStage = index;
@@ -62,14 +65,14 @@ void updateMountedFlag(
6265
for (index = lastIndexAfterFirstStage; index < newChildren.size(); index++) {
6366
const auto& newChild = newChildren[index];
6467
newChild->setMounted(true);
65-
updateMountedFlag({}, newChild->getChildren());
68+
updateMountedFlag({}, newChild->getChildren(), commitSource);
6669
}
6770

6871
// State 3: Unmount old children.
6972
for (index = lastIndexAfterFirstStage; index < oldChildren.size(); index++) {
7073
const auto& oldChild = oldChildren[index];
7174
oldChild->setMounted(false);
72-
updateMountedFlag(oldChild->getChildren(), {});
75+
updateMountedFlag(oldChild->getChildren(), {}, commitSource);
7376
}
7477
}
7578
} // namespace facebook::react

packages/react-native/ReactCommon/react/renderer/mounting/updateMountedFlag.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
#pragma once
99

1010
#include <react/renderer/core/ShadowNode.h>
11+
#include <react/renderer/mounting/ShadowTree.h>
1112

1213
namespace facebook::react {
1314
/*
1415
* Traverses the shadow tree and updates the `mounted` flag on all nodes.
1516
*/
1617
void updateMountedFlag(
1718
const ShadowNode::ListOfShared& oldChildren,
18-
const ShadowNode::ListOfShared& newChildren);
19+
const ShadowNode::ListOfShared& newChildren,
20+
ShadowTreeCommitSource commitSource);
1921
} // namespace facebook::react

0 commit comments

Comments
 (0)